specifing '-c path/pytest.ini' in pytest.main() only uses function pattern(python_functions) for one file
Question:
I what to call pytest.main this way:
pytest.main(['-c'.'pathpytest.ini', 'teststestfile1::src_func1','teststestfile2::src_func2'])
so basically I want to have the pytest.ini test outside the tests folder and call a list of tests from different files on the tests folder. I have defined a pattern for the test names in the pytest.ini: src_* in this example.
My problem is that the src_* pattern is only detected for first file that is the argument right next to the
'-c'.'path/pytest.ini', 'teststestfile1::src_func1'
and not for the
'teststestfile2::src_func2'
so the test in testfile2 is not found. The funny thing is that when the pytest.ini is inside the tests folder then I call pytest like
pytest.main(['teststestfile1::src_func1','teststestfile2::src_func2'])
and both tests on both files are detected, so the pattern src_* from the pytest.ini works as expected.
The question is why is the src_* not used for the second file when the pytest.ini is outside the tests folder?
I have debugged pytest and search online and until now I know the -c flag should be used to use a pytest.ini outside the test folder but for my application it doesnt work as it should, or Im using it the wrong way.
Answers:
the solution was to add –rootdir=pathtofoldertests as command line argument on the commands passed to pytest.main(), more explanation here : https://github.com/pytest-dev/pytest/discussions/10856#discussioncomment-5513661
I what to call pytest.main this way:
pytest.main(['-c'.'pathpytest.ini', 'teststestfile1::src_func1','teststestfile2::src_func2'])
so basically I want to have the pytest.ini test outside the tests folder and call a list of tests from different files on the tests folder. I have defined a pattern for the test names in the pytest.ini: src_* in this example.
My problem is that the src_* pattern is only detected for first file that is the argument right next to the
'-c'.'path/pytest.ini', 'teststestfile1::src_func1'
and not for the
'teststestfile2::src_func2'
so the test in testfile2 is not found. The funny thing is that when the pytest.ini is inside the tests folder then I call pytest like
pytest.main(['teststestfile1::src_func1','teststestfile2::src_func2'])
and both tests on both files are detected, so the pattern src_* from the pytest.ini works as expected.
The question is why is the src_* not used for the second file when the pytest.ini is outside the tests folder?
I have debugged pytest and search online and until now I know the -c flag should be used to use a pytest.ini outside the test folder but for my application it doesnt work as it should, or Im using it the wrong way.
the solution was to add –rootdir=pathtofoldertests as command line argument on the commands passed to pytest.main(), more explanation here : https://github.com/pytest-dev/pytest/discussions/10856#discussioncomment-5513661