I want to use pytest to do unit testing for the scripts in another folder called src
. Here is my directory structure:
src
__init__.py
script1.py
script2.py
test
test_fun.py
However, when I try to run pytest
in the test
folder through command line, I saw the following error
from script2 import fun2
E ModuleNotFoundError: No module named 'script2'
In each script, I have the following contents
script2.py:
def fun2():
return('result_from_2')
script1.py:
from script2 import fun2
def fun1():
return(fun2()+'_plus_something')
test_fun.py:
import sys
sys.path.append('../')
from src.script1 import fun1
def test_fun1():
output1 = fun1()
# check output
assert output1=='result_from_2_plus_something'
How can I run pytest with the directory provided above?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…