-
Hello everyone, I have a small question about pytest command line parameters. In my case, I want to open a .txt file, read some information from it and run some tests on these information. Since the name of the .txt file is not fixed, I want to pass it by command line. Also, I try to enclose all my test functions in a class, so that I only need to read the file once. Here is how I plan to implement this: @pytest.fixture(scope='class')
def setup_class(tester):
print("enter setup")
dir ='dataset/lidar'
tester=tester.split('.')[0]
with open(dir+'/result/'+tester+'.pcd.json','r',encoding='utf8')as f:
test=json.load(f)
with open(dir+'/gt/'+tester+'.json','r',encoding='utf8')as f:
standard=json.load(f)
return test, standard
class TestSimple:
def test_rotate_x(self,setup_class):
test,standard=setup_class
assert abs(test['extrinsic_param']['rotation'][0]-standard['extrinsic_param']['rotation'][0]) < 0.01 and in conftest.py, I write: def pytest_addoption(parser):
parser.addoption(
"--tester", action="store", default="lidar", help="my option: type1 or type2"
)
@pytest.fixture
def tester(request):
return request.config.getoption("--tester") But I got an error like ________________________________________________ ERROR at setup of TestSimple.test_rotate_x _________________________________________________
ScopeMismatch: You tried to access the 'function' scoped fixture 'tester' with a 'class' scoped request object, involved factories
test_front_lidar.py:8: def setup_class(tester)
conftest.py:8: def tester(request) Does anyone have used a command line parameter to initialize class variables? Or is there a better way to achieve this goal? Thank you very much to have a look. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
OK, it seems like adding one line suffice to solve my problem: def setup_class(request):
tester=request.config.getoption("--tester")
dir ='/dataset/lidar'
tester=tester.split('.')[0]
with open(dir+'/result/'+tester+'.pcd.json','r',encoding='utf8')as f:
test=json.load(f)
with open(dir+'/gt/'+tester+'.json','r',encoding='utf8')as f:
standard=json.load(f)
return test, standard |
Beta Was this translation helpful? Give feedback.
OK, it seems like adding one line suffice to solve my problem: