练习一: 使用unittest 完成自动化测试并使用HttpTestRunner生成报告
'''1、写个简单的计算器功能,大小写转换功能,随机生成字符串功能2、编写测试用例,不同的数据(你能想到的所有测试用例),并进行断言。除0的选择可以跳过skip,随机生成字符串功能可以断言是否包含你名字的缩写。3、使用unittest框架+HTMLTestRunner,最后生成html报告'''
import unittestimport osimport timeimport loggingimport ddtfrom HTMLTestRunner import HTMLTestRunnerimport randomtestData1 = [{'a':5,'b':1,'x':'+','result':6}, {'a':5,'b':1,'x':'-','result':4}, {'a':5,'b':1,'x':'*','result':5}, {'a':5,'b':1,'x':'/','result':5} ]testData2 = [{'string':'asd','stringType':'upper','result':'ASD'}, {'string': 'ASD', 'stringType': 'lower','result':'asd'} ]#被测函数class test_demo(): def jisuanqi(a,x,b): if x == '+': return a+b elif x =='-': return a-b elif x == '*': return a*b elif x == '/': return a/b else: logging.info('只支持数字加减乘除四则运算') def translation(string,stringType): if stringType == 'lower': return string.lower() elif stringType == 'upper': return string.upper() else: print('ERROR:只支持大小写类型转换') def random_string(num): return random.random(num)#测试函数@ddt.ddtclass TestCases(unittest.TestCase): @classmethod def setUpClass(cls): print('整个测试类运行前执行') def setUp(self): print("每个测试方法执行前运行一次") def tearDown(self): print("每个测试方法执行完后运行一次") @ddt.data(*testData1) def test_case_jisuanqi(self,data): result = test_demo.jisuanqi(data['a'],data['x'],data['b']) assert result == data['result'] @ddt.data(*testData2) def test_case_translation(self,data): result = test_demo.translation(data['string'],data['stringType']) assert result == data['result'] def test_case_randomString(self): pass @classmethod def tearDownClass(cls): print("整个测试类运行完成后执行一次")#测试报告if __name__ == '__main__': report_path = os.path.join(os.path.dirname(__file__), 'report') now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime()) filename = report_path + "/" + now + "_result.html" print('******************************************') suite = unittest.TestSuite() suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCases)) with open(filename, 'wb') as fp: runner = HTMLTestRunner( stream=fp, title='测试报告', description='测试用例') runner.run(suite)
练习二:使用pytest完成自动化测试并用allure生成测试报告
'''建立一个登陆模块的测试用例,一个人力资源模块的测试用例,其中包括增删改查4个小功能,查询不需要登陆。使用pytest,allure的方式,技术上要有登陆依赖用Fixture,conftest实现,要添加人员时有参数化,数据驱动,使用文件或list/dict的方式传入数据。通过allure的添加附加信息,及各种信息包括feature,story,step,attach,title,discription等。'''conftest.py
@pytest.fixture(scope = 'module')def test_login(request): user = request.param['user'] password = request.param['password'] if user != 'linda': print('用户名错误') elif password != '888888': print('密码错误') else: print('登陆成功') yield # 模块执行完case后 在最后执行一遍teardown操作。 print('执行teardown') print('推出登陆')
Pytest.py
import allureimport pytestimport osimport subprocesstest_user_data = [{"user": "linda", "password": "888888"}]test_user_data1 = [{"user": "linda", "password": "888888"}, {"user": "servenruby", "password": "123456"}, {"user": "linda", "password": "123456"}]test_user_data2 = [{"name": "中国平安", "money": 999, "country": 'china'}, {"name": "阿里巴巴", "money": 888, "country": 'jepan'}, {"name": "拼多多", "money": 666, "country": 'USA'}]@allure.feature('测试登录模块')@pytest.mark.run(order = 1) #第一个执行@pytest.mark.parametrize('test_login', test_user_data1, indirect=True)class TestLogin(): def test_login_case1(self,test_login): assert 1 == 1@allure.feature('测试人事模块')@pytest.mark.parametrize('test_login', test_user_data, indirect=True)class TestPersion(): @allure.story('测试用例:新增人员') @pytest.mark.parametrize('data',test_user_data2) def test_persion_add(self,test_login,data): with allure.step("步骤1"): allure.attach('说明') print('新增用户%s'%data['name']) @allure.story('测试用例:删除新增的人员') @pytest.mark.parametrize('data',test_user_data2) def test_persion_del(self,test_login,data): print('删除用户%s'%data['name'])