Python unittest discovery with subfolders
Question:
My unittest folder is organized this way.
.
|-- import
| |-- import.kc
| |-- import.kh
| `-- import_test.py
|-- module
| |-- module.kc
| |-- module.kh
| `-- module_test.py
`-- test.py
I’d want to simply run test.py
to run each of my *_test.py
using the unittest Python module.
Currently, my test.py contains
#!/usr/bin/env python
import unittest
if __name__ == "__main__":
suite = unittest.TestLoader().discover('.', pattern = "*_test.py")
unittest.TextTestRunner(verbosity=2).run(suite)
The python documentation says that it should automatically discover my test in the subfolders. But it does not.
At the moment, it only outputs
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
I’m sure it is not a problem with my *_test.py
file, because when I move them into the root directory, it works fine..
What am I doing wrong?
Answers:
Add __init__.py
in the import
and module
directories.
Consider using nose instead of the vanilla unittest
module, if you are able to switch. You won’t need to mess around with your own test.py file or anything; a run of nosetests
will find and run all your tests.
python -m unittest tests/**_test.py
My unittest folder is organized this way.
.
|-- import
| |-- import.kc
| |-- import.kh
| `-- import_test.py
|-- module
| |-- module.kc
| |-- module.kh
| `-- module_test.py
`-- test.py
I’d want to simply run test.py
to run each of my *_test.py
using the unittest Python module.
Currently, my test.py contains
#!/usr/bin/env python
import unittest
if __name__ == "__main__":
suite = unittest.TestLoader().discover('.', pattern = "*_test.py")
unittest.TextTestRunner(verbosity=2).run(suite)
The python documentation says that it should automatically discover my test in the subfolders. But it does not.
At the moment, it only outputs
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
I’m sure it is not a problem with my *_test.py
file, because when I move them into the root directory, it works fine..
What am I doing wrong?
Add __init__.py
in the import
and module
directories.
Consider using nose instead of the vanilla unittest
module, if you are able to switch. You won’t need to mess around with your own test.py file or anything; a run of nosetests
will find and run all your tests.
python -m unittest tests/**_test.py