1、parameterized.parameterized
import unittest from parameterized import parameterized,param class TestAdd(unittest.TestCase): ss = [ {'first': 1, 'second': 3, 'third': 5}, {'first': 4, 'second': 7, 'third': 8}, {"first": {'username': '15669910105'}}] @parameterized.expand([param(s) for s in ss]) #只能取出list的str元素 def test_add(self,data): print(data) print(data["first"]) if __name__ == '__main__': #Pycharm执行时注意鼠标需要放在 unittest.main(verbosity=2)代码块的位置,否则会报错 unittest.main(verbosity=2) # verbosity表示测试结果的信息复杂度
2、pytest.mark.parametrize
import pytest datas = [ {'first': 1, 'second': 3, 'third': 5}, {'first': 4, 'second': 7, 'third': 8}, {"first": {'username': '15669910105'}} ] @pytest.mark.parametrize("data",datas) def test_(data): print(data) if __name__ == '__main__': pytest.main(['-s', 'demo8.py']) #以print信息显示
3、ddt、unpack
import unittest from ddt import ddt,data,unpack datas = [ {'first': 1, 'second': 3, 'third': 5}, {'first': 4, 'second': 7, 'third': 8} ] @ddt class MyTesting(unittest.TestCase): def setUp(self): print('this is the setUp') @data(*datas) @unpack def test_dicts(self, first, second, third): print(first,second,third) def tearDown(self): print('this is tearDown') if __name__ == '__main__': unittest.main(verbosity=2)