pytest动态生成测试方法
pytest dynamically generate test method
你好我如何为列表或文件数量动态生成测试方法。
假设我有 file1、file2 和 filen,其输入值在 json 中。现在我需要 运行 对多个值进行相同的测试,如下所示,
class Test_File(unittest.TestCase):
def test_$FILE_NAME(self):
return_val = validate_data($FILE_NAME)
assert return_val
我正在使用以下命令 运行 py.test 生成 html 和 junit 报告
py.test test_rotate.py --tb=long --junit-xml=results.xml --html=results.html -vv
目前我手动定义的方法如下,
def test_lease_file(self):
return_val = validate_data(lease_file)
assert return_val
def test_string_file(self):
return_val = validate_data(string_file)
assert return_val
def test_data_file(self):
return_val = validate_data(data_file)
assert return_val
请告诉我如何指定 py 测试以在提供报告时动态生成 test_came 方法。
我很期待这个博客中提到的内容“http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases”
但是上面的博客使用了 unittest,如果我使用它,我将无法生成 html 和 junit 报告
当我们如下使用固定装置时,出现错误,例如它需要 2 个参数,
test_case = []
class Memory_utlization(unittest.TestCase):
@classmethod
def setup_class(cls):
fname = "test_order.txt"
with open(fname) as f:
content = f.readlines()
file_names = []
for i in content:
file_names.append(i.strip())
data = tuple(file_names)
test_case.append(data)
logging.info(test_case) # here test_case=[('dhcp_lease.json'),('dns_rpz.json'),]
@pytest.mark.parametrize("test_file",test_case)
def test_eval(self,test_file):
logging.info(test_case)
当我执行上面的命令时,出现以下错误,
> testMethod()
E TypeError: test_eval() takes exactly 2 arguments (1 given)
This 可能会帮助你。
你的测试 class 看起来像
class Test_File():
@pytest.mark.parametrize(
'file', [
(lease_file,),
(string_file,),
(data_file,)
]
)
def test_file(self, file):
assert validate_data(file)
你好我如何为列表或文件数量动态生成测试方法。 假设我有 file1、file2 和 filen,其输入值在 json 中。现在我需要 运行 对多个值进行相同的测试,如下所示,
class Test_File(unittest.TestCase):
def test_$FILE_NAME(self):
return_val = validate_data($FILE_NAME)
assert return_val
我正在使用以下命令 运行 py.test 生成 html 和 junit 报告
py.test test_rotate.py --tb=long --junit-xml=results.xml --html=results.html -vv
目前我手动定义的方法如下,
def test_lease_file(self):
return_val = validate_data(lease_file)
assert return_val
def test_string_file(self):
return_val = validate_data(string_file)
assert return_val
def test_data_file(self):
return_val = validate_data(data_file)
assert return_val
请告诉我如何指定 py 测试以在提供报告时动态生成 test_came 方法。
我很期待这个博客中提到的内容“http://eli.thegreenplace.net/2014/04/02/dynamically-generating-python-test-cases”
但是上面的博客使用了 unittest,如果我使用它,我将无法生成 html 和 junit 报告
当我们如下使用固定装置时,出现错误,例如它需要 2 个参数,
test_case = []
class Memory_utlization(unittest.TestCase):
@classmethod
def setup_class(cls):
fname = "test_order.txt"
with open(fname) as f:
content = f.readlines()
file_names = []
for i in content:
file_names.append(i.strip())
data = tuple(file_names)
test_case.append(data)
logging.info(test_case) # here test_case=[('dhcp_lease.json'),('dns_rpz.json'),]
@pytest.mark.parametrize("test_file",test_case)
def test_eval(self,test_file):
logging.info(test_case)
当我执行上面的命令时,出现以下错误,
> testMethod()
E TypeError: test_eval() takes exactly 2 arguments (1 given)
This 可能会帮助你。
你的测试 class 看起来像
class Test_File():
@pytest.mark.parametrize(
'file', [
(lease_file,),
(string_file,),
(data_file,)
]
)
def test_file(self, file):
assert validate_data(file)