import pytest
def test_example():
result = 2 + 2
assert result == 4How to write a basic test
On this page, we explain how to write a basic automated test in Python using pytest.
What is pytest?
Pytest is a popular framework for testing in Python, widely used in software development and data science.
You should install the pytest package into your environment from either conda or PyPI:
conda install pytest
# or
pip install pytestWhat a pytest test looks like
In pytest, any function whose name starts with test_ is treated as a test. Inside the function you write one or more assert statements. If an assertion fails, pytest will return an error message explaining what went wrong.
On this page, we explain how to write a basic automated test in R using testthat.
testthat
testthat is a popular framework for testing in R, widely used in software development and data science.
You should install the testthat package into your environment from CRAN.
install.packages("testthat")What a testthat test looks like
Tests are created using test_that(). They are built around expectations like expect_true(), expect_false(), expect_equal(), expect_error(), and others (see package index for more).
Simple test example
library(testthat)
test_that("2 add 2 equals 4", {
result <- 2L + 2L
expect_equal(result, 4L)
})Test passed with 1 success 😸.
A simple test for summary_stats
Here is a minimal example using the summary_stats function from the case study. For a single value, the function should return that value as the mean and NaN for the other statistics, because there is not enough data to define a standard deviation or confidence interval.
import pandas as pd
import pytest
def test_summary_stats_single_value():
"""Running summary_stats on a single value should only return mean."""
data = pd.Series([10])
mean, std, ci_lower, ci_upper = summary_stats(data)
assert mean == 10
assert pd.isna(std)
assert pd.isna(ci_lower)
assert pd.isna(ci_upper)#