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
PS: an alternatif: `pdbpp` (successor of `pytest-ipdb`) at: https://github.com/pdbpp/pdbpp
149
+
PS: an alternatif: `pdbpp` (successor of `pytest-ipdb`) at: <https://github.com/pdbpp/pdbpp>
143
150
144
151
## export PYTHONBREAKPOINT=ipdb.set_trace
145
152
@@ -235,6 +242,75 @@ pytest -k "not send_http" -v
235
242
pytest -k "send_http or quick" -v
236
243
```
237
244
245
+
## pytest -s
246
+
247
+
pytest captures stdout/stderr (including print) by default, and only shows that captured output when it's useful, usually on failures.
248
+
249
+
For e.g.,by default, we will only see the ouput of `setting up <function test_func2 at 0x785a4a11c180>` for `test_func2` only, not `test_func1`, even the print statement is in the setup_function, which is shared by both `test_func1` and `test_func2`. But with `pytest -s`, we will see the output for both tests.
250
+
251
+
So by default, without `-s` (same for `--capture=no`), print() output is hidden unless pytest decides to show it (typically for failing tests).
252
+
253
+
```python
254
+
# content of test_module.py
255
+
256
+
defsetup_function(function):
257
+
print("setting up", function)
258
+
259
+
deftest_func1():
260
+
assertTrue
261
+
262
+
deftest_func2():
263
+
assertFalse
264
+
```
265
+
266
+
Without `-s`, which is the defaut Pytest capture mode, all the print outputs are caapptured by Pytest, and is only displayed for the failed test `test_func2` (lines 16-17):
267
+
268
+
```bash title="pytest without -s" hl_lines="16-17" linenums="1"
Very similar to Python standard lib `unittest.mock.patch` decorator since Python 3, but `monkeypatch` is a fixture. Some people find `monkeypatch` is less effort to write than `unittest.mock.patch`. Ref. https://github.com/pytest-dev/pytest/issues/4576
485
+
Very similar to Python standard lib `unittest.mock.patch` decorator since Python 3, but `monkeypatch` is a fixture. Some people find `monkeypatch` is less effort to write than `unittest.mock.patch`. Ref. <https://github.com/pytest-dev/pytest/issues/4576>
410
486
411
487
To use the native `unittest.mock.patch`, use the [`wraps` parameter](https://stackoverflow.com/a/59460964/5095636):
412
488
@@ -583,7 +659,7 @@ pytest -n auto
583
659
584
660
## Ensure each pytest-xdist worker has its own database connection
585
661
586
-
Based on `worker_id` fixture, possible values are: `gw0`, `gw1`, etc., and `master` if no parallel fixture: https://breadcrumbscollector.tech/posts/running-tests-in-parallel-with-pytest/#worker_id-fixture
662
+
Based on `worker_id` fixture, possible values are: `gw0`, `gw1`, etc., and `master` if no parallel fixture: <https://breadcrumbscollector.tech/posts/running-tests-in-parallel-with-pytest/#worker_id-fixture>
587
663
588
664
## Showing the tests durations
589
665
@@ -616,7 +692,7 @@ mock.patch returns a mock object, a mock object can have whatever attributes and
616
692
# using simple speccing, mock.asssert_called_with() is detected as an error
0 commit comments