将数据从测试脚本中分离,减少维护成本
# 创建和 ‘‘‘ sina.json内容 login: notEamil: 请输入邮箱名 formatEmail: 您输入的邮箱名格式不正确 errorEmain: 登录名或密码错误 username: 用户名 password: 密码 sina.yaml内容 { "login": { "notEamil": "请输入邮箱名", "formatEmail": "您输入的邮箱名格式不正确", "errorEmain": "登录名或密码错误", "username": "用户名", "password": "密码" } } ‘‘‘
# 创建jsonUtils.py,将函数作为数据引入接口 # !/usr/bin/env python # -*- coding:utf-8 -*- import json from uiFrame.utils.pathUtils import filePath def readJson(): return json.load(open(filePath(directory=‘data‘, fileName=‘sina.json‘), encoding=‘utf-8‘)) # 读取json文件确认编码格式,避免格式不兼容
# 创建yamlUtils.py,将函数作为数据引入接口 # !/usr/bin/env python # -*- coding:utf-8 -*- import yaml from uiFrame.utils.pathUtils import filePath def readyaml(): return yaml.load(open(filePath(directory=‘data‘, fileName=‘sina.yaml‘), encoding=‘utf-8‘)) def getUrl(): return yaml.load(open(filePath(directory=‘config‘, fileName=‘config.yaml‘)))[‘url‘][‘qa‘]
# !/usr/bin/env python # -*- coding:utf-8 -*- import unittest from uiFrame.page.sina import Sina from uiFrame.page.Init import InitSian # 引入对应的分离数据库 from uiFrame.utils.yamalUtils import readyaml from uiFrame.utils.jsonUtils import readJson class SinaTest(InitSian, Sina): def test_sina_login_001(self): ‘‘‘登陆验证:用户名密码空‘‘‘ self.login(username=‘‘, password=‘‘) self.assertEqual(self.getDivText, readJson()[‘login‘][‘notEamil‘]) def test_sina_login_002(self): ‘‘‘登陆验证:用户名不规范‘‘‘ self.login(username=‘ASDFG‘, password=‘ASDFG‘) self.assertEqual(self.getDivText, readJson()[‘login‘][‘formatEmail‘]) def test_sina_login_003(self): ‘‘‘登陆验证:用户名密码错误‘‘‘ self.login(username=‘ASDFG@sina.com‘, password=‘ASDFG‘) self.assertEqual(self.getDivText, readJson()[‘login‘][‘errorEmain‘]) def test_sina_login_004(self): ‘‘‘登录判断是否登录‘‘‘ self.login( username=readyaml()[‘login‘][‘username‘], password=readyaml()[‘login‘][‘password‘] ) self.assertEqual(self.getNick, readyaml()[‘login‘][‘username‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
将测试网站url数据进行分离
创建config.yaml文件写入数据
url: qa: https://mail.sina.com.cn/
将测试固件中url参数改为url数据库函数
# !/usr/bin/env python # -*- coding:utf-8 -*- import unittest from selenium import webdriver from uiFrame.utils.yamalUtils import getUrl class InitSian(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get(getUrl()) self.driver.implicitly_wait(30) def tearDown(self) -> None: self.driver.quit()
pytest简单应用 # !/usr/bin/env python # -*- coding:utf-8 -*- def add(a, b): return a + b def test_add_001(): assert add(1, 2) == 2 def test_add_002(): assert add(‘h‘, ‘i‘) == ‘hi‘