Skip to content

Commit 8abf30a

Browse files
authored
Merge pull request #4155 from Tadaboody/Add_a_simple_example_on_how_to_use_pytester_to_the_CONTRIBUTING_guide_4151
Add testdir examples to CONTRIBUTING guide
2 parents 49defa2 + ea25eb1 commit 8abf30a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Thomas Hisch
209209
Tim Strazny
210210
Tom Dalton
211211
Tom Viner
212+
Tomer Keren
212213
Trevor Bekolay
213214
Tyler Goodlet
214215
Tzu-ping Chung

CONTRIBUTING.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,47 @@ Here is a simple overview, with pytest-specific bits:
280280
base: features # if it's a feature
281281

282282

283+
Writing Tests
284+
----------------------------
285+
286+
Writing tests for plugins or for pytest itself is often done using the `testdir fixture <https://docs.pytest.org/en/latest/reference.html#testdir>`_, as a "black-box" test.
287+
288+
For example, to ensure a simple test passes you can write:
289+
290+
.. code-block:: python
291+
292+
def test_true_assertion(testdir):
293+
testdir.makepyfile(
294+
"""
295+
def test_foo():
296+
assert True
297+
"""
298+
)
299+
result = testdir.runpytest()
300+
result.assert_outcomes(failed=0, passed=1)
301+
302+
303+
Alternatively, it is possible to make checks based on the actual output of the termal using
304+
*glob-like* expressions:
305+
306+
.. code-block:: python
307+
308+
def test_true_assertion(testdir):
309+
testdir.makepyfile(
310+
"""
311+
def test_foo():
312+
assert False
313+
"""
314+
)
315+
result = testdir.runpytest()
316+
result.stdout.fnmatch_lines(["*assert False*", "*1 failed*"])
317+
318+
When choosing a file where to write a new test, take a look at the existing files and see if there's
319+
one file which looks like a good fit. For example, a regression test about a bug in the ``--lf`` option
320+
should go into ``test_cacheprovider.py``, given that this option is implemented in ``cacheprovider.py``.
321+
If in doubt, go ahead and open a PR with your best guess and we can discuss this over the code.
322+
323+
283324
Joining the Development Team
284325
----------------------------
285326

changelog/4151.doc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tempir testing example to CONTRIBUTING.rst guide

0 commit comments

Comments
 (0)