The Python module unittest
seems to suggest a directory structure for a project that has a project root directory with source code and tests in that directory.
I would like, however, to write Python scripts in my directory ~/bin
and test it in another directory (say, ~/dev/tests
). Is there a way to run unit tests using the command line interface without setting my environment variable PYTHONPATH
and creating files __init__.py
and whatnot?
Here is a simple example demonstrating what I want:
~/bin/candy
:
def candy():
return "candy"
if __name__ == '__main__':
print candy()
~/dev/tests/test_candy.py
:
import unittest
import candy
class CandyTestCase(unittest.TestCase):
def testCandy(self):
candyOutput = candy.candy()
assert candyOutput == "candy"
I notice that everything can be done conveniently if:
- Two files are named with extensions py (
candy.py
and test_candy.py
) - Two files are in the same directory.
- :
$ python -m unittest test_candy
python unittest
, , :
- py (
~/candy
). - ,
test_candy
py . - ,
candy
test_candy.py
( ).
python -m unittest
, ?