Install

pip install pytest

Rules

  • Name all testing files with[ref] test_*.py or *_test.py.
  • Name all “be tested” functions with def test_*.

Executing

pytest test_sample.py
pytest -q test_sample.py # quiet mode

Simple

# test_sample.py
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

Then run pytest test_sample.py.

With parameters

# test_sample.py
@pytest.mark.parametrize(
    "input, param_1, param_2, result",
    [
        (df_1, 'date', np.mean, df_result_1),
        (df_2, 'date', np.mean, df_result_2),
    ]
)

def test_run(input, param_1, param_2, result):
    spts = SampleTimeSeries(param_1=param_1, param_2=param_2)
    df_tmp = spts.run(df)
    assert isinstance(df_tmp, pd.DataFrame)
    assert df_tmp.equals(result)

Errors

# ImportError: No module named atomicwrites
python -m pytest
python3 -m pytest

References