Ranjith Ramachandra
Ranjith Ramachandra

Reputation: 10764

How to override pytest.ini when running tests from the command line?

I have a codebase containing multiple python packages. There is also a pytest.ini file that contains the names of these files.

example directory structure:

main_dir
  |
  |
  |--- package1
  |
  |--- package2
  |
  |--- pytest.ini

pytest.ini looks like this

[pytest.ini]
addopts = package1 package2

The issue is because of pytest.ini I am not able to run tests just package wise. For example py.test package1\ now runs tests for package2 tests as well.

If I remove pytest.ini file, the command works as expected. Only option I see is to maintain an uncommitted version of pytest.ini which I keep changing according to my needs.

How do I override the pytest.ini settings and run tests only package wise?

Upvotes: 16

Views: 13772

Answers (3)

SilentGuy
SilentGuy

Reputation: 2203

You can use -o/--override-ini. From pytest help text:

 -o OVERRIDE_INI, --override-ini=OVERRIDE_INI
                    override ini option with "option=value" style, e.g. `-o
                    xfail_strict=True -o cache_dir=cache`.

But your situation will require another workaround since your addopts options are not in the form option=value since it is a positional argument.

#pytest.ini file
[pytest]
   addopts = 
    -k="package1 or package2"

And, while running tests, you can use pytest -o k=package1. -k is used to select tests based on expression.

Upvotes: 8

lmm333
lmm333

Reputation: 323

pytest -c MyTestGetJson.ini
pytest -c MyTestGetXML.ini

Upvotes: 4

user78110
user78110

Reputation:

addopts is meant for mostly configuration options, since it always adds the given options entirely

if all test folders are always added, then of course it always find all tests

Upvotes: 2

Related Questions