HDR UK Futures HDR UK Futures Testing in Research Workflows
  1. Introduction to writing and running tests
  2. How to write a basic test

This site contains materials for the testing module on HDR UK’s RSE001 Research Software Engineering training course. It was developed as part of the STARS project.

  • When and why to run tests?
  • Case study
  • Introduction to writing and running tests
    • How to write a basic test
    • How to run tests
    • Parameterising tests
  • Types of test
    • Functional tests
    • Unit tests
    • Back tests
  • Test coverage
  • Running tests via GitHub actions
  • Example repositories
  1. Introduction to writing and running tests
  2. How to write a basic test

How to write a basic test

Choose your language:  


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 pytest

What 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.

import pytest


def test_example():
    result = 2 + 2
    assert result == 4

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)
#
Case study
How to run tests
 
  • Code licence: MIT. Text licence: CC-BY-SA 4.0.