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.
Related
I made a small project called demo, with a single test in it
import unittest
class Test(unittest.TestCase):
def testName1(self):
self.assertEqual(5+9, 14)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
However, from command line
ThinkPad-T520:~/workspacep/demo$ python -m unittest
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Why doesn't this work? In general, how can I run all unit tests from command line with a single line?
The structure of the directory is
demo
tests
demo_test1.py __init__.py
There are three gotcha's that I know of:
Your tests in your TestCases need to be named test_*
Your test files need to be named: test*.py (by default, you can change it with the -p flag when running the tests). e.g. test_demo1.py
Your tests folder needs to have an __init__.py file in it, or else it won't be considered a valid location to import from.
So, for #1, you need to rename the test to test_name_1. And for #2, there's two options:
A - Restructure your files like this:
demo
tests
__init__.py
test_demo1.py
Then run python -m unittest and it should find the test cases.
B - Just run it like: python -m unittest discover -p *test.py
I fought with the same exact problem a while ago and I solved it by using test discovery command.
python -m unittest discover -s .
You can pass in your test file pattern as well and a whole other options https://docs.python.org/2/library/unittest.html#test-discovery
You need to pass in a list of modules.
For example, if your test file is foo.py, then you can run python -m unittest foo.
For me (running tests from IntelliJ IDEA) I had to remove the class' 'Run configuration'. Earlier on I had wrongly imported the unittest as _pytest.unittest and ran the test class. Of course that didn't work.
I corrected the module import, but the 'run configuration' was still there, causing it to run as a Python script and not as 'Python tests'.
I had a similar issue and figured out that I had to run python3 -m unittest instead as I had forgotten python defaults to Python 2 on my system
I have a made a simple unit test like this:
import unittest
class TestParsing(unittest.TestCase):
def test_vgm_1_01(self):
self.assertEqual('foo'.upper(), 'FOO')
if __name__ == '__main__':
unittest.main()
If I run it through PyDev's Run As -> Python Run, that is, if I run it as a normal program, relying on direct invocation of unittest.main(), it works exactly as expected:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
On the other hand, if it is executed via Run As -> Python unit-test, it simply does not work:
Finding files... done.
Importing test modules ... done.
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Does anyone have an idea of why wouldn't this be working as expected?
The current PyDev project is available at https://github.com/haroldo-ok/vgmparse/tree/support-vgm-1.01
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
Behold, my setup.py:
https://github.com/mongodb/motor/blob/master/setup.py
... and setup.cfg:
https://github.com/mongodb/motor/blob/master/setup.cfg
I'd like to be able to run one suite, like:
python setup.py nosetests test.test_motor_ssl
But I get "invalid command name 'test.test_motor_ssl'". With this, on the other hand:
python setup.py nosetests --tests test.test_motor_ssl
... nosetests runs every test in my project. How can I tell nosetests, when it is running in setup.py, how to run a subset of tests?
Apparently this is a known bug in nose 1.2.1 and they already have a fix in the master branch. You can either wait for the next version or use the nosetests command directly.
source: https://github.com/nose-devs/nose/issues/556
The only thing that works now is actually the directory approach. It is still not possible to specify the path...
python setup.py nosetests -w tests/test_folder
Using nose==1.3.1 I'm able to run a single test class/test case via:
python setup.py nosetests --tests tests/test_file.py:TestClass.test_case
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.