Skip to content

Commit 3d77db7

Browse files
authored
DOC: test organization (#37760)
1 parent 5844197 commit 3d77db7

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

doc/source/development/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Development
1616
code_style
1717
maintaining
1818
internals
19+
test_writing
1920
extending
2021
developer
2122
policies
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
.. _test_organization:
2+
3+
Test organization
4+
=================
5+
Ideally, there should be one, and only one, obvious place for a test to reside.
6+
Until we reach that ideal, these are some rules of thumb for where a test should
7+
be located.
8+
9+
1. Does your test depend only on code in ``pd._libs.tslibs``?
10+
This test likely belongs in one of:
11+
12+
- tests.tslibs
13+
14+
.. note::
15+
16+
No file in ``tests.tslibs`` should import from any pandas modules
17+
outside of ``pd._libs.tslibs``
18+
19+
- tests.scalar
20+
- tests.tseries.offsets
21+
22+
2. Does your test depend only on code in pd._libs?
23+
This test likely belongs in one of:
24+
25+
- tests.libs
26+
- tests.groupby.test_libgroupby
27+
28+
3. Is your test for an arithmetic or comparison method?
29+
This test likely belongs in one of:
30+
31+
- tests.arithmetic
32+
33+
.. note::
34+
35+
These are intended for tests that can be shared to test the behavior
36+
of DataFrame/Series/Index/ExtensionArray using the ``box_with_array``
37+
fixture.
38+
39+
- tests.frame.test_arithmetic
40+
- tests.series.test_arithmetic
41+
42+
4. Is your test for a reduction method (min, max, sum, prod, ...)?
43+
This test likely belongs in one of:
44+
45+
- tests.reductions
46+
47+
.. note::
48+
49+
These are intended for tests that can be shared to test the behavior
50+
of DataFrame/Series/Index/ExtensionArray.
51+
52+
- tests.frame.test_reductions
53+
- tests.series.test_reductions
54+
- tests.test_nanops
55+
56+
5. Is your test for an indexing method?
57+
This is the most difficult case for deciding where a test belongs, because
58+
there are many of these tests, and many of them test more than one method
59+
(e.g. both ``Series.__getitem__`` and ``Series.loc.__getitem__``)
60+
61+
A) Is the test specifically testing an Index method (e.g. ``Index.get_loc``,
62+
``Index.get_indexer``)?
63+
This test likely belongs in one of:
64+
65+
- tests.indexes.test_indexing
66+
- tests.indexes.fooindex.test_indexing
67+
68+
Within that files there should be a method-specific test class e.g.
69+
``TestGetLoc``.
70+
71+
In most cases, neither ``Series`` nor ``DataFrame`` objects should be
72+
needed in these tests.
73+
74+
B) Is the test for a Series or DataFrame indexing method *other* than
75+
``__getitem__`` or ``__setitem__``, e.g. ``xs``, ``where``, ``take``,
76+
``mask``, ``lookup``, or ``insert``?
77+
This test likely belongs in one of:
78+
79+
- tests.frame.indexing.test_methodname
80+
- tests.series.indexing.test_methodname
81+
82+
C) Is the test for any of ``loc``, ``iloc``, ``at``, or ``iat``?
83+
This test likely belongs in one of:
84+
85+
- tests.indexing.test_loc
86+
- tests.indexing.test_iloc
87+
- tests.indexing.test_at
88+
- tests.indexing.test_iat
89+
90+
Within the appropriate file, test classes correspond to either types of
91+
indexers (e.g. ``TestLocBooleanMask``) or major use cases
92+
(e.g. ``TestLocSetitemWithExpansion``).
93+
94+
See the note in section D) about tests that test multiple indexing methods.
95+
96+
D) Is the test for ``Series.__getitem__``, ``Series.__setitem__``,
97+
``DataFrame.__getitem__``, or ``DataFrame.__setitem__``?
98+
This test likely belongs in one of:
99+
100+
- tests.series.test_getitem
101+
- tests.series.test_setitem
102+
- tests.frame.test_getitem
103+
- tests.frame.test_setitem
104+
105+
If many cases such a test may test multiple similar methods, e.g.
106+
107+
.. code-block:: python
108+
109+
import pandas as pd
110+
import pandas._testing as tm
111+
112+
def test_getitem_listlike_of_ints():
113+
ser = pd.Series(range(5))
114+
115+
result = ser[[3, 4]]
116+
expected = pd.Series([2, 3])
117+
tm.assert_series_equal(result, expected)
118+
119+
result = ser.loc[[3, 4]]
120+
tm.assert_series_equal(result, expected)
121+
122+
In cases like this, the test location should be based on the *underlying*
123+
method being tested. Or in the case of a test for a bugfix, the location
124+
of the actual bug. So in this example, we know that ``Series.__getitem__``
125+
calls ``Series.loc.__getitem__``, so this is *really* a test for
126+
``loc.__getitem__``. So this test belongs in ``tests.indexing.test_loc``.
127+
128+
6. Is your test for a DataFrame or Series method?
129+
130+
A) Is the method a plotting method?
131+
This test likely belongs in one of:
132+
133+
- tests.plotting
134+
135+
B) Is the method an IO method?
136+
This test likely belongs in one of:
137+
138+
- tests.io
139+
140+
C) Otherwise
141+
This test likely belongs in one of:
142+
143+
- tests.series.methods.test_mymethod
144+
- tests.frame.methods.test_mymethod
145+
146+
.. note::
147+
148+
If a test can be shared between DataFrame/Series using the
149+
``frame_or_series`` fixture, by convention it goes in the
150+
``tests.frame`` file.
151+
152+
- tests.generic.methods.test_mymethod
153+
154+
.. note::
155+
156+
The generic/methods/ directory is only for methods with tests
157+
that are fully parametrized over Series/DataFrame
158+
159+
7. Is your test for an Index method, not depending on Series/DataFrame?
160+
This test likely belongs in one of:
161+
162+
- tests.indexes
163+
164+
8) Is your test for one of the pandas-provided ExtensionArrays (``Categorical``,
165+
``DatetimeArray``, ``TimedeltaArray``, ``PeriodArray``, ``IntervalArray``,
166+
``PandasArray``, ``FloatArray``, ``BoolArray``, ``StringArray``)?
167+
This test likely belongs in one of:
168+
169+
- tests.arrays
170+
171+
9) Is your test for *all* ExtensionArray subclasses (the "EA Interface")?
172+
This test likely belongs in one of:
173+
174+
- tests.extension

0 commit comments

Comments
 (0)