测试的模块:Demo1.py:
def screen(name,password): if(name =='peter'and password == '123456'): result='登录成功' return result else: result='登录失败' return result
SetJson.py:
import json list1=[{'name':'peter','password':'123456','result':'登录成功'} ,{'name':'tom','password':'123456','result':'登录失败'} ,{'name':'peter','password':'123457','result':'登录失败'}] with open('b.json','w',encoding='utf-8')as f: json.dump(list1,f,ensure_ascii=False,indent=3)
用例:b.json:
[ { "name": "peter", "password": "123456", "result": "登录成功" }, { "name": "tom", "password": "123456", "result": "登录失败" }, { "name": "peter", "password": "123457", "result": "登录失败" } ]
TestCase01.py:
import unittest import json from parameterized import parameterized from LearnUnitTest.Exercise.Exe_01.Demo01 import screen def data(): list1 = [] with open('b.json','r',encoding='utf-8')as f: res=json.load(f) for i in res: list1.append((i.get('name'),i.get('password'),i.get('result'))) return list1 class addCase(unittest.TestCase): # @unittest.skipIf('跳过') @parameterized.expand(data()) def test_mothed(self,name,password,result): self.assertEqual(result,screen(name,password))
TestSuite+TestLoader+HTMLTestRunner:
import unittest #前提:导入HTMLTestRunner文件 from HTMLTestRunner import HTMLTestRunner from LearnUnitTest.Exercise.Exe_01.u_TestCase01 import addCase ''' 1.TestSuite: Suite: suite=unittest.TestSuite() suite.addTest(unittest.makeSuite(addCase)) runner=unittest.TextTestRunner() runner.run(suite) ''' '''2.TestLoader: suite=unittest.TestLoader().discover('./','u_TestCase*.py') runner=unittest.TextTestRunner() runner.run(suite) ''' #3.HTMLTestRunner第三方报告: suite=unittest.defaultTestLoader.discover('.','u_TestCase01.py') file='report1.html' with open(file,'wb')as f: runner= HTMLTestRunner(f) runner.run(suite)