I'm trying to run pytest from within a script, so I want it to run all test files by simply invoking pytest with no arguments. However, pytest doesn't collect the testfiles.
Hirarchy is as follows
project
-somedirectory
--somecode.py
-tests
--test.py
test.py look as follows
def test_one():
assert True
Running pytest via the PyCharm GUI works as expected, test.py is collected, run and passed.
Running pytest tests/test.py works as expected, test.py is collected, run and passed.
Running pytest without arguments does not work as expected, it doesn't collect test.py.
Running pwd returns /path/to/project.
The behaviour is the same for Windows command prompt and the PyCharm terminal.
By default pytest looks for files with the pattern "test_*.py", your file "test.py" doesn't match this pattern so is likely being ignored. Try renaming it to something else? "test_foo.py" for example
This works. I did not realise the naming convention related to files as well as functions.
Related
I'm writing tests in python's unittest and decided to use the module parameterized to deal with test parameterization. Now all's fine and dandy when I'm running tests directly with unittest's CLI - simply running python -m unittest in root directory launches all the tests as expected. However, I decided for my script to have it's own command flag to run the tests, so when you run, say, python ./main.py -t [additional arguments for unittest], the script itself runs python -m unittest [additional arguments for unittest]. For that to happen, I'm using subprocess.run. And this also works... well, to some extent. The problem is the following - when I'm using python -m unittest, no errors (except for the ones being tested) are raised, but using my script to run the tests raises ModuleNotFoundError: No module named 'parameterized', along with a few other dependencies my code is using. I'm clueless why is that happening.
To be honest I'm not that familiar with unittest, so maybe my approach is the problem, maybe I should handle this in completely different way. Any feedback would be much appreciated.
Sometimes I want to print some statements, to make sure unittests are running fine (even if it passes), but can't find an option that enables it.
If tests fail, then it does show custom prints as output, but if it passes, it does ignore prints or logs (I mean, it dont see them on terminal output).
I tried using verbosity, like -vvvv, but it still ignores my prints. With nose there is an option like --nologcapture. Is there something similar in tox?
tox as such is just a generic venv creator, deps installer and command executor. It doesn't do output capturing (unless you use --parallel). So it is on a different abstraction level from nose. tox can run your tests or anything else that is runnable via command line.
Like you already mentioned for nose: depending on your test runner you might need to deactivate output capturing to see prints coming from the tests. So if you use pytest for example you can use pytest -s to disable all output capturing (also see docs)
You can also print something after you ran the test by adding something like this in your tox.ini testenv:
[testenv:test]
[...]
commands =
<some test command>
python -c 'print("All is fine."))'
In eclipse, i would like to know how to configure debug for single function.
I use pytest interpreter.
In my file 'hello.py', i have several function
How to make this without using console:
pytest hello.py::function
I'm setting up a Python Continuous Integration server, using Jenkins, and nosetests keeps running the same tests twice. I'm not importing the tests anywhere. Here's the command I'm running:
nosetests --with-xcoverage --with-xunit --all-modules --traverse-namespace --cover-package=app --cover-inclusive --cover-erase -x
Any ideas? It's a Flask-Restful app.
I had a similar issue. After turning up verbosity (as suggested by Schollii above) and comparing notes on this question what worked for me was deleting the init.py (and init.pyc of course) in my main code folder (of which tests were a subdirectory).
I just had this one. Apparently I messed up the command line syntax. It's not:
nosetests module.py module.class_name
It's:
nosetests module.class_name
I'm using PyDev ( with Aptana ) to write and debug a Python Pylons app, and I'd like to step through the tests in the debugger.
Is it possible to launch nosetests through PyDev and stop at breakpoints?
Here is what i do to run nosetests using eclipse Pydev (Hope this will help you).
first of all i create a python script and i put it in the root of my package directory :
--Package
|
| -- runtest.py
|
| -- ... (others modules)
and in runtest.py i put:
import nose
nose.main()
now i go to in the menu Run -> Run configurations and i create a new configuration of Pydev Django i choose my package and put runtest.py in the main Module , next i go to arguments tab in the same widget and i put in Program arguments the path to my project and different arg to pass to the script example:
/home/me/projects/src --with-doctest # Run doctests too
now after clicking on Apply i can run this configuration .
For debugging you can run this configuration in debug mode and put your break point anywhere in your code and you can use the terrific debug widget to do several action : step into, to see vars ...
N.B : for doctests sadly i don't think you can put breakpoint in the line of doctest but what you can do is to put your breakpoint in the def of the function that is called by the doctest and like that you can use the debug mode .
Try import pydevd; pydevd.settrace() where would like a breakpoint.
I got this working, somewhat - that is, I don't have breakpoints and stepping working but I do get PyDev to run the tests and show the results in the PyUnit view.
When you run the unit test you'll have to override the test runner to use "nose" and command line arguments "--with-pylons=path/to/test.ini" in the arguments tab of the run configuration. For example I set it to "--with-pylons=../../test.ini". Unfortunately I have to set this up separately for each test I run, I haven't found a way to put a variable or project path in there.
Also, unfortunately, I haven't been able to get breakpoints working. I tried patching as recommended in http://pydev.blogspot.ca/2007/06/why-cant-pydev-debugger-work-with.html and its comments to no avail. YMMV.
In DecoratorTools-1.8-py2.7.egg/peak/util/decorators.py in decorate_assignment(), replace:
oldtrace = [frame.f_trace]
with
oldtrace = [sys.gettrace()]