tribbloid
tribbloid

Reputation: 3838

pytest: how to override command line option in python program?

By default, pytest inflates the error traceback massively and printly some information into sysout stream that are redundant: Considering that I'm using PyCharm, it is really obfuscating to see code snippet out of context, while they are already available in the IDE & debugging interface.

As a result, I intend to set pytest traceback to native permanently. However, according to the documentation, the only way to do so is to add extra command line argument when launching the test runner:

-tb=native

I would like to make my test to always use the native traceback regardless of how it was run. Is it possible to use a TestCase API to do so?

Thanks a lot for your help.

Upvotes: 1

Views: 976

Answers (2)

Lord Elrond
Lord Elrond

Reputation: 16032

I'm not sure how you can do this using pytest, nor am I familiar with this package. With that being said, you can always create a bash function to accomplish this:

function pytest() {
  pytest -tb=native "$@"
}

The "$@" symbol will pass all arguments following pyt to the function (kind of like *args in python), so running pyt arg1 arg2 ... argn will be the same as running pytest -tb=native arg1 arg2 ... argn

If you are unfamiliar with creating bash shortcuts, see this question.

Update

I misunderstood and thought OP was calling pytest from the cli. Instead of creating the pyt function, if you override pytest directly, PyCharm might invoke your bash version of it instead (I'm not really sure though).

That being said, yaniv's answer seems superior to this, if it works.

Upvotes: 0

yeniv
yeniv

Reputation: 1639

You can add this option to the pytest.ini file and it would be automatically picked by pytest. For your specific case, a pytest.ini with following contents should work:

[pytest]
addopts = --tb=native

Note the double hyphens with tb; I am using pytest 4.6.4 and that is how it works for me.

Also, Refer pytest docs for another alternative by modifying PYTEST_ADDOPTS env variable.

Upvotes: 3

Related Questions