Skip to content

DOC: testing how-to use fixtures / parametrize #15608

Closed
@jreback

Description

@jreback

I think might be nice to update: http://pandas-docs.github.io/pandas-docs-travis/contributing.html#writing-tests

with how to make / use fixtures and how to use parametrize. something like (and mention that we cannot do this in a class TestClass(...) structure)

Its nice for a beginner to write this up because it will then be from there perspective :>

(pandas) bash-3.2$ more tester.py
import pytest
import numpy as np
import pandas as pd


@pytest.mark.parametrize('dtype', ['int8', 'int16', 'int32', 'int64'])
def test_dtypes(dtype):
    assert str(np.dtype(dtype)) == dtype

@pytest.fixture
def series():
    return pd.Series([1, 2, 3])

@pytest.fixture(params=['int8', 'int16', 'int32', 'int64'])
def dtype(request):
    return request.param

def test_series(series, dtype):
    series = series.astype(dtype)
    assert series.dtype == dtype

and running it

((pandas) bash-3.2$ pytest  tester.py  -v
============================================================================================== test session starts ===============================================================================================
platform darwin -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0 -- /Users/jreback/miniconda3/envs/pandas/bin/python
cachedir: .cache
rootdir: /Users/jreback/pandas, inifile: setup.cfg
plugins: cov-2.4.0, xdist-1.15.0
collected 8 items 

tester.py::test_dtypes[int8] PASSED
tester.py::test_dtypes[int16] PASSED
tester.py::test_dtypes[int32] PASSED
tester.py::test_dtypes[int64] PASSED
tester.py::test_series[int8] PASSED
tester.py::test_series[int16] PASSED
tester.py::test_series[int32] PASSED
tester.py::test_series[int64] PASSED

and lastly about using tm.assert_series_equal and such.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsTestingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions