ModuleNotFoundError with pytest

后端 未结 6 717
时光说笑
时光说笑 2021-02-12 11:05

I want my tests folder separate to my application code. My project structure is like so

myproject/
  myproject/
    myproject.py
    moduleone.py
  tests/
    my         


        
6条回答
  •  醉话见心
    2021-02-12 11:22

    So it seems that the sys.path has to include the application directory rather than the project root folder containing the application directory and test directory.

    So in my case /my/local/path/myproject/myproject/ had to be in sys.path rather than /my/local/path/myproject/.

    Then I could run pytest in /my/local/path/myproject/ (didn't need python -m pytest). This meant that the modules within /myproject/myproject/ could find each other and the tests as well without any namespace nesting.

    So my tests looked like

     from moduleone import ModuleOne
     import pytest
    
     def test_fun():
         assert ModuleOne.example_func() == True
    

    That said, there seem to be many gotchas, so I have no idea if this is correct..

提交回复
热议问题