Skip to content

Unit test

Pytest

Unittest Python native module

https://docs.python.org/3/library/unittest.mock.html

very useful because you can easily replace the module call within the test target

e.g. you can pretend a module/methods return value within the test target, without the need to modify it.

Setup

  1. Better to start with this structure. A separated test folders
  2. ~~Create an empty __init__.py in proj_root/tests/~~
    1. ~~Now you can run pytest and import the src files correctly~~
    2. ~~BUT, the import inside src files, that you imported, still not work.~~
  3. Create a file [conftest.py](http://conftest.py) in proj_root/tests/ with below content:

    import os
    import sys
    
    sys.path.append(
        os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + "/../src/")
    )
    

    So the src folder is also added to sys path

Comments