一、断言详解
from selenium import webdriver import unittest class BaiduTest(unittest.TestCase): def setUp(self) -> None: self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(‘http://www.baidu.com‘) def tearDown(self) -> None: self.driver.quit() def test_baidu_title(self): # 验证百度的title是否是“百度一下,你就知道” self.assertEqual(self.driver.title,‘百度一下,你就知道‘) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
(2)assertTrue返回的是bool类型,也就是对被测试的对象进?验证,如果返回的是boolean类型并且是true,那么结果验证通过,那么?法assertFlase()验证的是被测试对象返回的内容是false。
from selenium import webdriver import unittest class BaiduTest(unittest.TestCase): def setUp(self) -> None: self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(‘http://www.baidu.com‘) def tearDown(self) -> None: self.driver.quit() def test_baidu_so(self): # 验证百度搜索框是否可被编辑 so=self.driver.find_element_by_id(‘kw‘) # self.assertEqual(so.is_enabled(),True) self.assertTrue(so.is_enabled()) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
(3)assertIn()值的是?个值是否包含在另外?个值??,在这?特别的强调?下,在assertIn()的?法??,有两个参数,那么值的包含其实就是第?个实际参数包含第?个实际参数。与之相反的?法是assergNotIn()
from selenium import webdriver import unittest class BaiduTest(unittest.TestCase): def setUp(self) -> None: self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(‘http://www.baidu.com‘) def tearDown(self) -> None: self.driver.quit() def test_baidu_in(self): # assertIn()值的是一个值是否包含在另外一个值里面 self.assertIn(‘百度一下‘, self.driver.title) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
(4)断?中的注意事项
from selenium import webdriver import unittest class BaiduTest(unittest.TestCase): def setUp(self) -> None: self.driver=webdriver.Chrome() self.driver.maximize_window() self.driver.get(‘http://www.baidu.com‘) def tearDown(self) -> None: self.driver.quit() def test_baidu_if(self): title = self.driver.title if title == ‘百度一下,你就知道‘: print(‘测试通过‘) else: print(‘测试不通过‘) def test_baidu_try(self): # 不正确的断言,测试报告显示通过,但测试点实际上没有通过 title = self.driver.title try: self.assertEqual(title, ‘百度一,你就知道‘) except Exception as e: print(e.args[0]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
二、单元测试框架的优化(Json)
例:新浪邮箱的登录
import os def base_dir(): return os.path.dirname(os.path.dirname(__file__)) # """获取当前工程的路径""" def filePath(directory=‘data‘,fileName=None): # 获取文件路径 return os.path.join(base_dir(),directory,fileName)
出现错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xae in position 92: illegal multibyte sequence
获取不到sina.json文件内容的解决办法:1、设置IDE的编码为UTF-8 2、读取文件的时候,设置编码
import json from utils.pathUtils import base_dir,filePath import os def readJson(): return json.load(open(filePath(directory=‘data‘,fileName=‘sina.json‘),encoding=‘utf-8‘)) print(readJson())
输出为字典格式 {‘login‘: {‘notEmail‘: ‘请输入邮箱名‘, ‘formatEmail‘: ‘您输入的邮箱名格式不正确‘, ‘errorEmail‘: ‘登录名或密码错误‘, ‘username‘: ‘wuya1303@sina.com‘, ‘password‘: ‘admin123‘, ‘shouYe‘: ‘邮箱首页‘, ‘title‘: ‘新浪邮箱‘, ‘url‘: ‘https://m0.mail.sina.com.cn/classic/index.php#title=%25E9%2582%25AE%25E7%25AE%25B1%25E9%25A6%2596%25E9%25A1%25B5&action=mailinfo‘}}
from page.sina import Sina from page.init import InitSina from utils.jsonutils import readJson import unittest class sinaTest(InitSina,Sina): def test_sina_001(self): self.login(username=‘‘,password=‘‘) self.assertEqual(self.getDivText,readJson()[‘login‘][‘notEmail‘]) def test_sina_002(self): self.login(username=‘5566..oo‘,password=‘‘) self.assertEqual(self.getDivText,readJson()[‘login‘][‘formatEmail‘]) def test_sina_003(self): self.login(username=‘15730996037‘,password=‘asd‘) self.assertEqual(self.getDivText,readJson()[‘login‘][‘errorEmail‘]) def test_sina_004(self): self.login(username=readJson()[‘login‘][‘username‘], password=readJson()[‘login‘][‘password‘]) self.assertEqual(self.getShouye,readJson()[‘login‘][‘shouYe‘]) def test_sina_005(self): self.login(username=readJson()[‘login‘][‘username‘], password=readJson()[‘login‘][‘password‘]) self.assertEqual(self.getNick,readJson()[‘login‘][‘username‘]) def test_sina_006(self): self.login(username=readJson()[‘login‘][‘username‘], password=readJson()[‘login‘][‘password‘]) self.assertEqual(self.getTitle,readJson()[‘login‘][‘title‘]) def test_sina_007(self): self.login(username=readJson()[‘login‘][‘username‘], password=readJson()[‘login‘][‘password‘]) self.assertEqual(self.getUrl, readJson()[‘login‘][‘url‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
例:新浪邮箱的登录
import os def base_dir(): return os.path.dirname(os.path.dirname(__file__)) # """获取当前工程的路径""" def filePath(directory=‘data‘,fileName=None): # 获取文件路径 return os.path.join(base_dir(),directory,fileName)
import yaml from utils.pathUtils import filePath import os def readYaml(): return yaml.load(open(filePath(fileName=‘sina.yaml‘),encoding=‘utf-8‘)) print(readYaml()) {‘login‘: {‘notEmail‘: ‘请输入邮箱名‘, ‘formatEmail‘: ‘您输入的邮箱名格式不正确‘, ‘errorEmail‘: ‘登录名或密码错误‘, ‘username‘: ‘wuya1303@sina.com‘, ‘password‘: ‘admin123‘}}
from page.sina import Sina from page.init import InitSina from utils.yamlUtils import readYaml from utils.yamlUtils import getUrl import unittest class sinaTest(InitSina,Sina): def test_sina_001(self): self.login(username=‘‘,password=‘‘) self.assertEqual(self.getDivText,readYaml()[‘login‘][‘notEmail‘]) def test_sina_002(self): self.login(username=‘5566..oo‘,password=‘‘) self.assertEqual(self.getDivText,readYaml()[‘login‘][‘formatEmail‘]) def test_sina_003(self): self.login(username=‘15730996037‘,password=‘asd‘) self.assertEqual(self.getDivText,readYaml()[‘login‘][‘errorEmail‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
四、网址的分离 创建config.yaml文件写入网址
import os def base_dir(): return os.path.dirname(os.path.dirname(__file__)) # """获取当前工程的路径""" def filePath(directory=‘data‘,fileName=None): # 获取文件路径(默认参数) return os.path.join(base_dir(),directory,fileName)
import yaml from utils.pathUtils import filePath import os def readYaml(): return yaml.load(open(filePath(directory=‘data‘,fileName=‘sina.yaml‘),encoding=‘utf-8‘)) print(readYaml()) def getUrl(): return yaml.load(open(filePath(directory=‘config‘,fileName=‘config.yaml‘),encoding=‘utf-8‘))[‘url‘][‘qa‘] print(getUrl()) {‘login‘: {‘notEmail‘: ‘请输入邮箱名‘, ‘formatEmail‘: ‘您输入的邮箱名格式不正确‘, ‘errorEmail‘: ‘登录名或密码错误‘, ‘username‘: ‘wuya1303@sina.com‘, ‘password‘: ‘admin123‘}} https://mail.sina.com.cn/
import unittest from selenium import webdriver from utils.yamlUtils import getUrl class InitSina(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初步应用
Pytest?起unittest来说?较?由,使?unittest?先要继承TestCase的类,但是pytest是不需要的,安装成功后,直接编写测试函数或者测试?法就可以使?了。
安装的命令为:
原文:https://www.cnblogs.com/wuyikai/p/15176850.html