Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable pytest dumping out source code?

Tags:

python

pytest

When one of the tests fail, pytest will dump out the source code of the function where the exception is raised. However, something when the error is raised from another library, it still dumps the function source code flood the output.

Is it possible to disable pytest from dump source code and have the stack trace only? Stack trace is usually more than enough to track down the problem.

I have searched a bit but all I can find are posts related to --show-capture.

like image 864
Joshua Avatar asked Nov 23 '18 06:11

Joshua


People also ask

How do I turn off pytest?

Disable Pytest for you projectOpen the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework. In the Default test runner field select Unittests. Click OK to save the settings.

What does pytest fail do?

If you call pytest. fail inside a test, it's a FAIL status for that test (or XFAIL, if it has xfail mark). But, if you call it from a fixture, the test, which depends on this fixture, is getting ERROR state.

What is Pytest_configure?

pytest allows you to create custom plugins for this sort of functionality. But it also allows you to register those same plugin hooks in your conftest.py files. The above code in your tests/conftest.py file will do the trick. pytest_configure() registers a new marker ( focus ).

How do I run a specific test in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests.


2 Answers

You can use the --tb option. You can choose either --tb=short or --tb=native as per what suits you. Check the detailed documentation here.

like image 87
Samarth Avatar answered Oct 17 '22 02:10

Samarth


Here is how to put your chosen option in a pytest.ini file:

[pytest]
# --tb not given    Produces reams of output, with full source code included in tracebacks
# --tb=no           Just shows location of failure in the test file: no use for tracking down errors
# --tb=short        Just shows vanilla traceback: very useful, but file names are incomplete and relative
# --tb=native       Slightly more info than short: still works very well. The full paths may be useful for CI
addopts = --tb=native

The available values for --tb are:

  --tb=style            traceback print mode
                        (auto/long/short/line/native/no).

Useful links:

  • pytest Configuration file formats
  • pytest Command-line Flags
like image 27
Clare Macrae Avatar answered Oct 17 '22 03:10

Clare Macrae