Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jreback opened this issue Mar 7, 2017 · 4 comments · Fixed by #15883
Closed

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

jreback opened this issue Mar 7, 2017 · 4 comments · Fixed by #15883
Labels
Docs Testing pandas testing functions or related to the test suite
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Mar 7, 2017

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.

@jreback jreback added Difficulty Novice Docs Testing pandas testing functions or related to the test suite labels Mar 7, 2017
@jreback jreback added this to the Next Major Release milestone Mar 7, 2017
@jorisvandenbossche
Copy link
Member

+1 That would be really useful.
I was also personally not yet familiar with pytest, as have to say it is somewhat complex for new contributors/people not familiar with pytest.

What is the reason that it does not work within a Class?

@jreback
Copy link
Contributor Author

jreback commented Mar 7, 2017

So we have almost all tests defined like this (was the nose style)

class TestFoo(tm.TestCase):

    def test_abc(....):
        self.assertTrue(...)
        self.assertRaises(...)

so this will (and is working) with pytest. But, you cannot use parametrize with this (because ultimately TestCase inherits from unittest.TestCase.

So we have to make these functional (e.g. independent functions), or inherit from object. But THEN we lose the ability to use self.assertTrue(...). However these are easily replaced by

# self.assertTrue
assert this == that

# self.assertRaises(....)
with pytest.raises(....): 
   

@toobaz
Copy link
Member

toobaz commented Mar 10, 2017

http://pandas.pydata.org/developers.html#testing needs an update too I guess

@jreback
Copy link
Contributor Author

jreback commented Mar 10, 2017

actually that entire section should simply be a re-direct / rename to http://pandas.pydata.org/pandas-docs/stable/contributing.html that is really old text.

@toobaz you can do a PR if you'd like to: https://github.com/pandas-dev/pandas-website if you'd like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants