testing
unittest
Say you have a project named deadbeef, then the following directory structure seem to do the job quite well:
deadbeef/
βββ src
β βββ anothermodule
β β βββ anotherscript.py
β βββ module
β βββ script.py
βββ test
βββ __init__.py
βββ test_anothermodule
β βββ __init__.py
β βββ test_anotherscript.py
βββ test_module
βββ __init__.py
βββ test_script.pyWhere an example of how test_script.pycould be:
import unittest
import src.module.script
class TestScript(unittest.TestCase):
def test_plus_one(self):
res = src.module.script.plus_one(0)
self.assertEqual(res, 1)
if __name__ == '__main__':
unittest.main()This way, the structure is to a high degree self-explanatory. The source code for each module is separated from the test code and represented in the same structure, except for the use of test_*. Also, new modules and tests can be added as necessary without hassle.
Tests can then be run from deadbeef/utilizing automatic discovery as such:
pytest
Same directory structure as above can be used, and if it is preferred to go without __init__.py files, then execution is done as:
Last updated