Skip to content

Commit 36e6e34

Browse files
committed
Format fixes and improvements in commandlineuseful.rst
1 parent 1eb953e commit 36e6e34

File tree

1 file changed

+63
-45
lines changed

1 file changed

+63
-45
lines changed

doc/en/new-docs/user/commandlineuseful.rst

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ Direct argument to select tests
88
-s, --capture=no
99
----------------
1010

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.
1214

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:
1516

17+
.. code-block:: python
18+
19+
# file test_documentation.py
1620
def test_fail():
1721
print "this test is going to fail"
1822
assert False
@@ -22,66 +26,76 @@ If the following piece of code, saved in a file named ``test_documentation.py``
2226
print "this test is going to pass"
2327
assert True
2428
25-
What will be printed will in the terminal be the following::
2629
27-
test_documentation.py .F
30+
::
31+
32+
$ pytest
33+
test_documentation.py .F
2834

29-
=================================== FAILURES ===================================
30-
__________________________________ test_fail ___________________________________
35+
=================================== FAILURES ===================================
36+
__________________________________ test_fail ___________________________________
3137

3238
def test_fail():
3339
print "this test is going to fail"
34-
> assert False
35-
E assert False
40+
> assert False
41+
E assert False
3642

37-
test_documentation.py:7: AssertionError
38-
----------------------------- Captured stdout call -----------------------------
39-
this test is going to fail
40-
====================== 1 failed, 1 passed in 0.02 seconds ======================
43+
test_documentation.py:7: AssertionError
44+
----------------------------- Captured stdout call -----------------------------
45+
this test is going to fail
46+
====================== 1 failed, 1 passed in 0.02 seconds ======================
47+
48+
You can see that no ``print`` statement is displayed, except for the failing test.
4149

42-
But if it is run with the parameter (``py.test test_documentation.py -s``), the result showed will be the following one::
50+
Now with the ``-s`` option::
4351

44-
====================================================== test session starts ======================================================
45-
test_documentation.py this test is going to fail
46-
F this test is going to pass
47-
.
52+
$ pytest -s
53+
====================================================== test session starts ======================================================
54+
test_documentation.py this test is going to fail
55+
F this test is going to pass
56+
.
4857

49-
=========================================================== FAILURES ============================================================
50-
___________________________________________________________ test_fail ___________________________________________________________
58+
=========================================================== FAILURES ============================================================
59+
___________________________________________________________ test_fail ___________________________________________________________
5160

5261
def test_fail():
5362
print "this test is going to fail"
54-
> assert False
55-
E assert False
63+
> assert False
64+
E assert False
5665

57-
test_documentation.py:7: AssertionError
58-
============================================== 1 failed, 1 passed in 0.02 seconds ===============================================
66+
test_documentation.py:7: AssertionError
67+
============================================== 1 failed, 1 passed in 0.02 seconds ===============================================
5968

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.
6170

6271
-k EXPRESSION
6372
-------------
6473

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.
6682

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().
6983

7084
-v, --verbose
7185
-------------
7286

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::
7689

77-
================================================================================ test session starts ================================================================================
90+
$ pytest -v
91+
=========================== test session starts ================================
7892
collected 2 items
7993

8094
test_documentation.py::test_pass PASSED
8195
test_documentation.py::test_fail FAILED
8296

83-
===================================================================================== FAILURES ======================================================================================
84-
_____________________________________________________________________________________ test_fail _____________________________________________________________________________________
97+
================================= FAILURES =====================================
98+
________________________________ test_fail _____________________________________
8599

86100
def test_fail():
87101
print "this test is going to fail"
@@ -91,29 +105,34 @@ For example, if the same test sets of the other examples is run with that argume
91105
test_documentation.py:7: AssertionError
92106
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
93107
this test is going to fail
94-
======================================================================== 1 failed, 1 passed in 0.03 seconds =========================================================================
108+
====================== 1 failed, 1 passed in 0.03 seconds ======================
109+
95110

96-
So it collects before run.
97111

98112
--collect-only
99113
--------------
100114

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::
102118

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 ======================
104125

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.
110128

111129
-x, --exitfirst
112130
---------------
113131

114132
Exit instantly after the first failure.
115133

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.
117136

118137
--lf, --last-failed
119138
-------------------
@@ -127,4 +146,3 @@ For example, in the test file above, if it is run first without any arguments, a
127146

128147
Shows a list with all command options.
129148

130-
See also command line options reference [TO-DO]

0 commit comments

Comments
 (0)