我试图用来pytest
检查一个函数——这是我的代码:
# src/return_self.py
def return_self(n):
return n
# tests/return_self_test.py
import pytest
def test_1():
value = return_self(1)
assert value == 1
我如何在我的src
文件中要求以便我可以测试它pytest
?我尝试了几件事:
1. import return_self
2. from src.return_self import *
3. import sys
sys.path.append('../src')
4. import imp
return_self = imp.load_source('return_self', '/source/return_self.py')
我也试过__init__.py
在根和src
目录中有和没有文件。但每次,我都会在 error 上得到一些变化E ModuleNotFoundError: No module named 'return_self'
。我如何在我的文件中要求?
你可以试试这个方法:
# tests/return_self_test.py
import os
import sys
import pytest
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from src.return_self import return_self
def test_1():
value = return_self(1)
assert value == 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句