run nosetests in all subdirectories - python

I can run tests in workflow folder with nosetests:
workflow maks$ nosetests
..........
----------------------------------------------------------------------
Ran 10 tests in 0.093s
OK
my tests live in test folder:
workflow maks$ ls
__pycache__ iterations.py test
data iterationsClass.py testData
env iterationsClass.pyc
But when I move to parent dir:
(py3env)Makss-Mac:workflow maks$ cd ..
It cannot find tests.
(py3env)Makss-Mac:Packages maks$ nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.005s
OK
So how to make nosetest search tests in all subdirectories?

If you make your workflow folder a module by placing __init__.py in it, nose should be able to find your tests.

If you don't want to make your folder a module, put a setup.cfg in the directory you want to run nosetests which automatically passes the tests option to nosetests to specify relative location of tests. Like so:
#setup.cfg contents:
[nosetests]
exe = True
tests = workflow/
You can also pass multiple folders/tests using this same mechanism:
#setup.cfg contents:
[nosetests]
exe = True
tests = workflow/, other/dir/full/of/tests/, direct/file/my_test.py

Related

pytest-cov - Don't count coverage for the directory of integration tests

I have the following directory structure:
./
src/
tests/
unit/
integration/
I would like to use pytest to run all of the tests in both unit/ and integration/, but I would only like coverage.py to calculate coverage for the src/ directory when running the unit/ tests (not when running integration/ tests).
The command I'm using now (calculates coverage for all tests under tests/):
pytest --cov-config=setup.cfg --cov=src
with a setup.cfg file:
[tool:pytest]
testpaths = tests
[coverage:run]
branch = True
I understand that I could add the #pytest.mark.no_cover decorator to each test function in the integration tests, but I would prefer to mark the whole directory rather than to decorate a large number of functions.
You can attach markers dynamically. The below example does that in the custom impl of the pytest_collection_modifyitems hook. Put the code in a conftest.py in the project root dir:
from pathlib import Path
import pytest
def pytest_collection_modifyitems(items):
no_cov = pytest.mark.no_cover
for item in items:
if "integration" in Path(item.fspath).parts:
item.add_marker(no_cov)

How to run PyBuilder unit tests in sub directories

Has anyone worked out how to run a pybuilder unit test, using the default configuration, whereby the _tests.py file is in a sub directory(s).
I can get the test to run when in the default successfully.
OK
src/unittest/python/my_tests.py
NOT OK
src/unittest/python/a/b/c/my_tests.py
I've tried with and without the __init__.py files in the unit test directories, no joy.

Emulating "nosetests -w working-dir" behavior using py.test

I am porting a series of tests from nosetests + python unittest to py.test. I was pleasantly surprised to learn that py.test supports python unittests and running existing tests with py.test is just as easy as calling py.test instead of nosetests on the command line. However I am having problems with specifying the working directory for the tests. They are not in the root project dir but in a subdir. Currently the tests are run like this:
$ nosetests -w test-dir/ tests.py
which changes the current working directory to test-dir and run all the tests in tests.py. However when I use py.test
$ py.test test-dir/tests.py
all the tests in tests.py are run but the current working directory is not changed to test-dir. Most tests assume that the working directory is test-dir and try to open and read files from it which obviously fails.
So my question is how to change the current working directory for all tests when using py.test.
The are a lot of tests and I don't want to invest the time to fix them all and make them work regardless of the cwd.
Yes, I can simply do cd test-dir; py.test tests.py but I am used to working from the project root directory and don't want to cd every time I want to run a test.
Here is some code that may give you better idea what I am trying to achieve:
content of tests.py:
import unittest
class MyProjectTestCase(unittest.TestCase):
def test_something(self):
with open('testing-info.txt', 'r') as f:
test something with f
directory layout:
my-project/
test-dir/
tests.py
testing-info.txt
And then when I try to run the tests:
$ pwd
my-project
$ nosetests -w test-dir tests.py
# all is fine
$ py.test ttest-dir/tests.py
# tests fail because they cannot open testing-info.txt
So this is the best I could come up with:
# content of conftest.py
import pytest
import os
def pytest_addoption(parser):
parser.addoption("-W", action="store", default=".",
help="Change current working dir before running the collected tests.")
def pytest_sessionstart(session):
os.chdir(session.config.getoption('W'))
And then when running the tests
$ py.test -W test-dir test-dir/tests.py
It's not clean but it will do the trick until I fix all the tests.

Why doesn't nosetests find anything?

I am switching from python's unittest framework to nosetests, trying to reuse my unittest.TestCases
After cding into my tests package I started nosetests as described on their homepage:
./test/$ nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Why do I need to specify each module to have nose discover its tests like in the following example?
./test/$ nosetests test_all.py
.......
----------------------------------------------------------------------
Ran 7 tests in 0.002s
OK
Also running the tests one folder above doesn't change anything.
./tests/$ cd ..
./$ nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
I can see in your repo that at least some of the files are executable, so that is at least part of the problem. By default, nose won't collect those: it's trying to avoid running scripts that might do something destructive on import. Try the --exe flag, or removing the executable bit from the test files.
You need to be in the directory above that if you want nose to run all the tests in that package.
In my case I had following line at the end of the test files:
unittest.main()
Removing this from all my tests solved my issue.

Cannot run tests with nosetests

I am trying to use nosetests to run all test cases inside my project.
I cd to my project directory:
cd projects/myproject
Then I do:
nosetests
Which outputs:
----------------------------------------------------------------------
Ran 0 tests in 0.001s
OK
Inside projects/myproject I have a package called Encode and inside the package I have Test directory with tests:
Encode/
__init__.py
Video.py
Ffmpeg.py
Test/
VideoTest.py
FfmpegTest.py
Why is nosetests not detecting my unit tests? All of my unit test classes extend unittest.TestCase.
From the nose docs:
Any function or class that matches the configured testMatch regular expression ((?:^|[\b_\.-])[Tt]est) by default – that is, has test or Test at a word boundary or following a - or _) and lives in a module that also matches that expression will be run as a test.
Your tests aren't being found because your filenames don't match the pattern. Change them to Video_Test.py, or Test_Video.py, for example. BTW: It's also odd that they have camelCase names like that, but that won't stop them from working.
nose won't find your tests if they are buried in a subdirectory. You should be able to tell it where to look though. Try nosetests --where=Encode/Test.

Categories