Using nose to test mixed python2 and python3 code - python

I have a project that has both python2 and python3 code in it (its a client/server thing; the server is python3 but there are python2 and python3 clients). I'm using nose to handle unit testing. The directory layout is (currently) basically like
client2
tests
client3
tests
server
tests
Is there a way to set up or use nose so that running node with python2 gets just the python2 tests, and similarly python3. For instance, something like:
client2
tests2
client3
tests3
server
tests3
... with some suitable nose arguments. I messed around with the -m option but couldn't get anywhere.

Looks like you will need a combo of --where and --py3where:
--py3where:
Look for tests in this directory under Python 3.x. Functions the same
as 'where', but only applies if running under Python 3.x or above.
Note that, if present under 3.x, this option completely replaces any
directories specified with 'where', so the 'where' option becomes
ineffective. [NOSE_PY3WHERE]
So for you it would be something like this:
nosetests --where=. --where=client2 --py3where=. --py3where=client3 --py3where=server -v
When using nosetests with python3 it will run client3 and server tests only. When running on python2 it will run client2 tests only.
Also, have a look at this question and answers for alternatives.

Related

Is it possible to run pdb and unittest python modules at the same time?

For example:
python3 -m unittest -m pdb test/test_string.py
or
python3 -m unittest pdb test/test_string.py
So ideally I'd like python to start in debug mode on exceptions, but without add any extra code into the files.
If you install Twisted and use Twisted Trial as you runner, you can do it like this:
trial --debug test/test_string.py
Tangentially, it's better if you put your tests in an importable package and name them by module name rather than filename (eg test.test_string instead of test/test_string.py).

ModuleNotFoundError when running unittest with subprocess

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.

Python tox: show stdout/prints on successful test run?

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."))'

Python nosetests runs tests twice

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

How calculate the global coverage?

I am using tox to test my python egg. And I want to know the coverage.
But the problem is that the tests are executing with python 2 (2.6 and 2.7) and python 3 (3.3) and some lines should be executed in python 2 and other in python 3, but this look like if only count the lines that are executed with python 2 (the last section in the tox, py26-dj12). You can see this here:
https://coveralls.io/files/64922124#L33
Of this way pass with the differents django version...
Is there some way to get the global coverage?
Yesterday I receipted an email answering this question:
coverage.py (the tool coveralls uses to measure coverage in Python programs) has a "coverage combine" command.
Yesterday, I got the global coverage executing something like this:
coverage erase
tox
coverage combine
coveralls
In tox.ini I added the "p" param:
python {envbindir}/coverage run -p testing/run_tests.py
python {envbindir}/coverage run -p testing/run_tests.py testing.settings_no_debug
I fixed the problem with these commits:
https://github.com/Yaco-Sistemas/django-inplaceedit/commit/200d58b2170b9122369df73fbfe12ceeb8efd36c
https://github.com/Yaco-Sistemas/django-inplaceedit/commit/bf0a7dcfc935dedda2f23d5e01964e27f01c7461

Categories