I don’t do tests, but I’d like to start. I have some questions:
-
Is it ok to use the
unittestmodule for that? From what I understand, theunittestmodule will run any method starting withtest. -
if I have a separate directory for the tests ( consider a directory named
tests), how would I import the code I’m testing? Do I need to use theimpmodule? Here’s the directory structure :src/
tests/
It’s fine to use
unittest. This module will run methods beginning withtestfor classes which inherit fromunittest.TestCase.There’s no need to use the
impmodule – your test module would just import the code under test as normal. You might need to add thesrcdirectory to your path:This code would be in your test module(s) before any import of your modules.
A better approach is to use the OS environment variable
PYTHONPATH.(Windows)
SET PYTHONPATH=path\to\module; python test.py(Linux)
PYTHONPATH=path/to/module; python test.pyAn alternative to
unittestis nose.