I am trying to run a specific test method with pytest on Python 3.6 with the following command:
pytest sanity_test.py::test_check
(as mentioned in pytest docs)
and it fails with the following error message:
ERROR: not found: <rootdir>/tests/automation/sanity/sanity_test.py::test_check
(no name '<rootdir>/tests/automation/sanity/sanity_test.py::test_check' in any of [<DoctestModule 'tests/automation/sanity/sanity_test.py'>, <Module
'tests/automation/sanity/sanity_test.py'>])
when I run the whole test file the tests are running properly with:
pytest sanity_test.py
When I tried running a specific method in isolated project it works perfectly.
Any reason why pytest can't find specifically my test methods on my project?
The file is very simple and looks like this:
def test_check():
pass
Thank to hoefling, I recognized that I had --doctest-modules option in my setup.cfg file. Removing this option fixed the problem.
hoefling opened an issue in pytest for that problem.
Related
I have a file main.py which calls "pytest.main()"
I have a file "pytest.ini" which contains the "testpaths" = "my_test_file.py" and a marker for dependency
"my_test_file.py" looks kind of like this:
import pytest
class TestClass(object):
#pytest.mark.dependency()
def test_a(self):
assert False
#pytest.mark.dependency(depends=["test_a"])
def test_b(self):
pass
When I run main.py, I get expected behavior, 1 test failed, 1 test skipped. All good.
Now I use "pyinstaller" to create an executable from my file main.py. When I run the main.exe, I get 1 test failed 1 test passed as an output. test_b seems to ignore the decorator and does NOT skip. I use the following pyinstaller command:
pyinstaller --onefile main.py
What am I doing wrong that invokes python to ignore my decorator in the executable?
Also best practice suggestions will be greatly appreciated here.
Ultimate goal: A pytest executable single file, that when executed, runs a bunch of tests. If the very first test (connectivity test) fails, all other tests should be skipped. Works in my local environment but not after making an executable.
If you want to reproduce the problem, I am using the following versions:
python 3.7.5
pytest==6.1.0
pytest-dependency==0.5.1
pyinstaller==4.0
pyinstaller-hooks-contrib==2020.9
I'm currently learning unittesting, and I have stumbled upon a strange error:
If I run my script from inside PyCharm, everything works perfectly. If I run it from my cmd.exe (as administrator), I get the following error:
This is my code:
import unittest
class TutorialUnittest(unittest.TestCase):
def test_add(self):
self.assertEqual(23,23)
self.assertNotEqual(11,12)
# function for raising errors.
def test_raise(self):
with self.assertRaises(Exception):
raise Exception`
Just remove the .py extension.
You are running your tests using the -m command-line flag. The Python documentation will tell you more about it, just check out this link.
In a word, the -m option let you run a module, in your case the unittest module. This module expect to receive a module path or a class path following the Python format for module path (using dots). For example, if you want to run the FirstTest class in the mytests module in a mypackage folder you would use the following command line:
python -m unittest mypackage.mytests.FirstTest
Assuming that you are running the previous command line from the parent folder of mypackage. This allows you to select precisely the tests you want to run (even inside a module).
When you add the .py extension, unittest is looking for a py object (like a module or a class) inside the last element of the module path you gave but, yet this object does not exist. This is exactly what your terminal error tells:
AttributeError: ’module’ object has no attribute ’py’
you can add at the bottom of your script:
if __name__ == "__main__":
unittest.main()
Then you can run python test_my_function.py normally
I am new to pytest and I have python2.6 installed on my setup.
I installed pytest and the testcases get executed properly. I installed couple of plugins like pytest-timeout, putest-xdist etc but these plugins does not load when I run the cases. For timeout, I get following error: py.test: error: unrecognized arguments: --timeout
Same steps followed with python2.7 works.
Any idea how this can be solved or alteast steps to debug to know what exactly is causing the issue.
Unfortunately pytest < 3.0 "hides" the ImportError happening when failing to import a plugin. If you remove all plugin arguments but add -rw, you should be able to see what exactly is going wrong in the warning summary.
In your conftest.py file just add the following line after the imports:
pytest_plugins = 'pytest_timeout'
It should solve your issue.
How can I generate test report using pytest? I searched for it but whatever i got was about coverage report.
I tried with this command:
py.test sanity_tests.py --cov=C:\Test\pytest --cov-report=xml
But as parameters represents it generates coverage report not test report.
Ripped from the comments: you can use the --junitxml argument.
$ py.test sample_tests.py --junitxml=C:\path\to\out_report.xml
You can use a pytest plugin 'pytest-html' for generating html reports which can be forwarded to different teams as well
First install the plugin:
$ pip install pytest-html
Second, just run your tests with this command:
$ pytest --html=report.html
You can also make use of the hooks provided by the plugin in your code.
import pytest
from py.xml import html
def pytest_html_report_title(report)
report.title = "My very own title!"
Reference: https://pypi.org/project/pytest-html/
I haven't tried it yet but you can try referring to https://github.com/pytest-dev/pytest-html. A python library that can generate HTML output.
py.test --html=Report.html
Here you can specify your python file as well. In this case, when there is no file specified it picks up all the files with a name like 'test_%' present in the directory where the command is run and executes them and generates a report with the name Report.html
You can also modify the name of the report accordingly.
I have a problem running nose tests and get results inside Jenkins.
The job has a shell script like this:
. /var/lib/jenkins/envs/itravel/bin/activate
python api/manage.py syncdb --noinput
DJANGO_SETTINGS_MODULE=ci_settings nosetests --verbosity=0 --processes=1 --with-xunit --xunit-file=nosetests.xml
deactivate
Part of the test suite is run using the django_nose.NoseTestSuiteRunner.
All the tests are run and the resulting nosetests.xml file is created but does not seem to be filled with the tests results:
<?xml version="1.0" encoding="UTF-8"?><testsuite name="nosetests" tests="0" errors="0" failures="0" skip="0"></testsuite>
I noticed that on an import Error fail the file is filled with one error, but otherwise, nothing...
Any idea? Is there something special to do from the tests side? Any property to set or so?
Thanks.
As far as I know, the --processes option is not compatible with --with-xunit. When you ask nosetests to run with the processes plugin, the tests are run in specified number of subprocesses. The xunit plugin does not know how to gather results into the xml file.
Just remove the --processes option and you should be fine.
Nose has had an open and unresolved GitHub issue for this since 2011. As #sti said, everything works fine if you don't use --processes. For everyone else, consider using Ignas/nose_xunitmp instead:
pip install nose_xunitmp
nosetests --with-xunitmp
nosetests --xunitmp-file results.xml