You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/en/new-docs/user/commandlineuseful.rst
+63-45Lines changed: 63 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,15 @@ Direct argument to select tests
8
8
-s, --capture=no
9
9
----------------
10
10
11
-
Normally stdout is only showed for failing tests. ``-s`` shows stdout calls, for example the print statement of all the tests.
11
+
Normally stdout and stderr are captured and only shown for failing tests.
12
+
The ``-s`` option can be used to disable capturing, showing stdcalls
13
+
for print statements, logging calls, etc.
12
14
13
-
An example of how that works can be the following one:
14
-
If the following piece of code, saved in a file named ``test_documentation.py`` is run with no parameter (if you run in the terminal, ``py.test test_documentation.py``).::
15
+
Consider the following code and pytest execution:
15
16
17
+
.. code-block:: python
18
+
19
+
# file test_documentation.py
16
20
deftest_fail():
17
21
print"this test is going to fail"
18
22
assertFalse
@@ -22,66 +26,76 @@ If the following piece of code, saved in a file named ``test_documentation.py``
22
26
print"this test is going to pass"
23
27
assertTrue
24
28
25
-
What will be printed will in the terminal be the following::
============================================== 1 failed, 1 passed in 0.02 seconds ===============================================
66
+
test_documentation.py:7: AssertionError
67
+
============================================== 1 failed, 1 passed in 0.02 seconds ===============================================
59
68
60
-
The biggest difference in the both statements is the part entitled "Tests session starts", where it prints the statements "this test is going to pass" (so the test that passed), and the "this test is going to fail".
69
+
You can see that no output is captured, and ``print`` statements are displayed normally.
61
70
62
71
-k EXPRESSION
63
72
-------------
64
73
65
-
EXPRESSION is a key word to select a subset of tests to be run.
74
+
EXPRESSION is a keyword to select a subset of tests to be run.
75
+
76
+
Using the same file from the previous exception as an example, you can use::
77
+
78
+
$ pytest -k pass
79
+
80
+
81
+
To filter only test names that contains ``pass``, so only ``test_pass()`` is executed.
66
82
67
-
An example of this using the same file as above could be the following one:
68
-
if this file is run without the parameter (if you run in the terminal, ``py.test test_documentation.py``), both tests on the file would be run. If you run that with the parameter (typing, for example ``py.test test_documentation.py -k pass``), it would filter only the tests names that contains the expression ``pass``, so it would run only the test_pass().
69
83
70
84
-v, --verbose
71
85
-------------
72
86
73
-
It shows more details of the tests, for example the test names.
74
-
75
-
For example, if the same test sets of the other examples is run with that argument, the results are going to be the following ones::
87
+
Enables verbose mode, displaying full test names instead of only ``.`` in the
88
+
terminal::
76
89
77
-
================================================================================ test session starts ================================================================================
90
+
$ pytest -v
91
+
=========================== test session starts ================================
======================================================================== 1 failed, 1 passed in 0.03 seconds =========================================================================
108
+
====================== 1 failed, 1 passed in 0.03 seconds ======================
109
+
95
110
96
-
So it collects before run.
97
111
98
112
--collect-only
99
113
--------------
100
114
101
-
Shows a list of the tests without running them. It can be useful in combination with ``-k``.
115
+
Shows a list of the tests without running them
116
+
117
+
for example, in the test file above, if it was run with the collect-only argument, it would display as a result something like the bellow file::
102
118
103
-
for example, in the test file above, if it was run with the collect-only argument, it would display as a result something like the bellow file:
119
+
$ pytest --collect-only
120
+
collected 2 items
121
+
<Module 'test_documentation.py'>
122
+
<Function 'test_fail'>
123
+
<Function 'test_pass'>
124
+
====================== no tests ran in 0.00 seconds ======================
104
125
105
-
collected 2 items
106
-
<Module 'test_documentation.py'>
107
-
<Function 'test_fail'>
108
-
<Function 'test_pass'>
109
-
=========================================================================== no tests ran in 0.00 seconds ======================
126
+
Note that ``--collect-only`` can be used with ``-k`` to see which tests are selected
127
+
by the expression.
110
128
111
129
-x, --exitfirst
112
130
---------------
113
131
114
132
Exit instantly after the first failure.
115
133
116
-
For example, in the test file above, if it is run with the --exitfirst argument, it would run only the ``test_fail``, and not the ``test_pass``, because it would exit after having the first failure.
134
+
Using the previous file as an example, running with ``--exitfirst`` will only
135
+
execute up to ``test_fail``, and not the ``test_pass``, because it would exit after having the first failure.
117
136
118
137
--lf, --last-failed
119
138
-------------------
@@ -127,4 +146,3 @@ For example, in the test file above, if it is run first without any arguments, a
0 commit comments