Is there a way to point pytest at a differnt pytest.ini file?
The application I am working on has one pytest.ini file that is used for running unittests when it spins up, and I do not want to modify that file.
I do however want to point pytest at a different file when running my automation tests via pytest.main()
pytest.main(['-p', 'no:django', '-v', '--json-report', '-m', test_marker])
Is there a way to tell pytest to use a pytest.ini file from another location?
$ pytest --help | grep -F config
-c file load configuration from `file` instead of trying to
locate one of the implicit configuration files.
That is,
pytest.main(['-c', 'pytest-alt.ini'])
Related
Suppose the pytest config file pyproject.toml is in the project directory:
[tool.pytest.ini_options]
addopts = "-rap --capture=tee-sys"
I usually use the options in this file when running the command pytest. And the options are automatically read from the file.
However, sometimes I want to run pytest with -rA only:
pytest -rA # it becomes: pytest -rA --capture=tee-sys
I have to run it with: pytest -rA --capture=fd to override the option --capture=tee-sys in the config file.
Is there any way to force pytest to read options from commandline only?
What I've tried:
In #Andrei 's answer of this question: Is there an option for pytest to ignore the setup.cfg file?
Use -c /dev/null will prevent pytest from finding the config file. I also find that set -c any-non-exist-path is ok.
However, I'm afraid if -c /dev/null has side effects. In pytest docs (https://docs.pytest.org/en/latest/reference/customize.html#finding-the-rootdir), it says If -c is passed in the command-line, use that as configuration file, and its directory as rootdir. I don't know what will happen if rootdir is set as a non exist dir.
Need help!
I have a job on Gitlab ci, that runs tests and reruns failed ones. If there are no failed tests, job fails with exit code 5, that means that there are no tests for running. I found out that there is plugin "pytest-custom_exit_code", but I don't know how to correctly use it.
I need just to add command 'pytest --suppress-no-test-exit-code' to my runner.sh?
It looks like this now:
#!/bin/sh
/usr/local/bin/pytest -m test
/usr/local/bin/pytest -m --last-failed --last-failed-no-failures none test
Assumption here is that plugin is installed first using
pip install pytest-custom_exit_code
command like option pytest --suppress-no-test-exit-code should work after that.
If configuration file like .pytest.ini is used , following lines should be added in it
[pytest]
addopts = --suppress-no-test-exit-code
My file structure is below,
Logic
--packageA
----file1
----file2
--packageB
----file3
----file4
--tests
----testfile1
----testfile2
----testfile3
--.coveragerc
I'd like to include packageA only for tests, and run testfile1 and testfile2 to measure the coverage.
testfile3 is for packageB.
So I wrote my .coveragerc file,
[run]
branch = True
include = /packageA/*
omit = *tests*
and, when I run coverage with command coverage run --rcfile=../.coveragerc -m pytest in directory tests, it tries running testfile3 as well.
How can I run testfile1 and testfile2 with .coveragerc configuration?
You can use the [run] command_line option to set the command line to use when you run coverage run. But coverage isn't trying to be a general runner. You might want a shell script, a Makefile, or a tox.ini file instead.
Are you typing the coverage run --rcfile=../.coveragerc -m pytest command by hand?
I have several test files and I run them one by one using a .bat file.
The batch file looks like this:
py.test --html=Myreport.html -v testfile1.py --tb=short
py.test --html=Myreport.html -v testfile2.py --tb=short
py.test --html=Myreport.html -v testfile3.py --tb=short
The problem is that each test run will overwrite Myreport.html report (so in this case the report will contain only tests present in testfile3.py file as those from the other two files were overwritten).
Is there any way to have all the test results in a single HTML file report?
Thanks
I currently writing a python cli application that takes in a CSV file as an argument and various options from the cli.
python .\test.py data.csv
usage: test.py [-h] [-v | -q] [-o] filepath
I want to alias or replace the python ./test in the cli with another word so as to look like a command like angular or git cli. For example rexona data.csv -o.
I want to do that on both Windows and Linux so as I can publish it as a PyPI distribution.
Thank you
Aliasing is a very OS and environment-dependent and is not the correct way to achieve what you are looking for.
Instead, you should use the tools offered by the packaging tool you are using to create the distributed package.
For example, if using setup.py then add
entry_points={
'console_scripts': ['rexona = path.to.module:function_name']
},
to the call to setup(...).