Skip to content

Commit a3d6ad8

Browse files
authored
DOC: test organization (#37637)
1 parent c156b12 commit a3d6ad8

File tree

3 files changed

+153
-0
lines changed

3 files changed

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

pandas/tests/series/methods/test_item.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
"""
2+
Series.item method, mainly testing that we get python scalars as opposed to
3+
numpy scalars.
4+
"""
15
import pytest
26

37
from pandas import Series, Timedelta, Timestamp, date_range
48

59

610
class TestItem:
711
def test_item(self):
12+
# We are testing that we get python scalars as opposed to numpy scalars
813
ser = Series([1])
914
result = ser.item()
1015
assert result == 1

0 commit comments

Comments
 (0)