![]() |
Unit Testing is an essential part of software development where we check whether the individual part of the code is running as intended. This helps in increasing the efficiency of the code. R is a statistical programming language that provides multiple such libraries and frameworks that are easy to use. This article will discuss one such unit testing framework in R called “tinytest”. What is tinytest?tinytest is a lightweight, dependency-free unit testing framework for R Programming Language. It offers a straightforward way to write and run tests, making it a great choice for both small scripts and larger projects. It is simple and easy to use yet it offers many powerful tools to work with tinytest giving a simple approach to unit testing while covering all essential features. It supports.
Installation of tinytestWe can simply install this framework like any other package in R using install.packages(). # Install package
install.packages("tinytest")
# Load package
library(tinytest) Implementation of tinytesttinytest can be used for various reasons: 1. Testing FunctionsOne of the primary uses of tinytest is to test individual functions in R.
Output: ----- PASSED : <-->
call| expect_equal(add(1, 1), 2, info = "Adding 1 and 1 should equal 2")
info| Adding 1 and 1 should equal 2
----- PASSED : <-->
call| expect_equal(add(-1, 1), 0, info = "Adding -1 and 1 should equal 0")
info| Adding -1 and 1 should equal 0
----- PASSED : <-->
call| expect_equal(add(1.5, 2.5), 4, info = "Adding 1.5 and 2.5 should equal 4")
info| Adding 1.5 and 2.5 should equal 4 2. Testing Statistical ModelsThis framework can also be used to test statistical model in R.
Output: ----- PASSED : <-->
call| expect_true(abs(coef(model)[2] - 2) < 0.5, info = "Slope should be approximately 2")
info| Slope should be approximately 2 3. Testing Package FunctionsThis package can also test the other package function and whether they are working properly or not.
Output: ----- PASSED : <-->
call| expect_equal(hello("Alice"), "Hello, Alice", info = "Hello function should greet properly")
info| Hello function should greet properly 4. Testing Error HandlingEnsuring that functions handle errors and edge cases properly is critical. tinytest can test that functions raise appropriate errors when given invalid inputs.
Output: ----- PASSED : <-->
call| expect_equal(divide(6, 3), 2, info = "6 divided by 3 should equal 2")
info| 6 divided by 3 should equal 2
----- PASSED : <-->
call| expect_error(divide(1, 0), "Division by zero", info = "Division by zero should raise an error")
info| Division by zero should raise an error 5. Testing Web Scraping and API FunctionsWhen working with web scraping or APIs, it’s important to test that the functions can handle various responses and edge cases correctly.
Output: ----- PASSED : <-->
call| expect_error(fetch_data("invalid_url"), "Invalid URL", info = "Invalid URL should raise an error")
info| Invalid URL should raise
----- PASSED : <-->
call| expect_equal(fetch_data("http://example.com"), data.frame(id = 1,
call| value = 42), info = "Should fetch data correctly")
info| Should fetch data correctly Best PracticesThere are certain steps that should be taken in order to avoid issues in unit testing using tinytest:
ConclusionThis article explored the possible usage of tinytest package and framework with suitable examples. This gave a descriptive code for better understanding. tinytest-FAQsCan tinytest be used with other testing frameworks?
What should I do if a test fails?
How do I handle random outputs in tests?
|
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |