unittest结构:
base:基础类
config:配置文件
data:存储测试数据的
image:存储图片文件的
log:存储日志文件
page:对象层
report:存储测试报告
test:测试模块
utils:工具类
main.py:程序执行的入口
一、断言
正确断言的三种方式
1、url断言
在base下创建一个对url的封装
from selenium.webdriver.common.by import By # from selenium.webdriver.support.expected_conditions import NoSuchElementException # from selenium.webdriver.support.wait import WebDriverWait from selenium import webdriver class WebDriver: def __str__(self): return ‘driver‘ def findElement(self,*loc): #*loc return self.driver.find_element(*loc) def findElements(self,*loc): return self.driver.find_elements(*loc) def findFrame(self, frameID): return self.driver.switch_to.frame(frameID) @property def getTitle(self): return self.driver.title @property def getUrl(self): return self.driver.current_url
2、在data下从创建一个json
写入新网址
3、在执行测试用例的时候调用这个方法
import time as t from page.sina import Sina from page.init import InitSina from utils.joinUtils import readJson import unittest class QQTest(InitSina,Sina): def test_sina_click_006(self): """点击登陆成功3""" self.login( readJson()[‘login‘][‘username‘], readJson()[‘login‘][‘password‘]) self.assertEqual(self.getUrl,readJson()[‘login‘][‘url‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
nick和test验证(page下的sina.py)页面层
from selenium.webdriver.common.by import By from base.base import WebDriver import time as t class Sina(WebDriver): username_loc=(By.ID,‘freename‘) password_loc=(By.ID,‘freepassword‘) login_loc=(By.LINK_TEXT,‘登录‘) divText=(By.XPATH,‘/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]‘) divText1=(By.XPATH,‘/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[2]‘) divText2=(By.LINK_TEXT,‘注册‘) nick=(By.XPATH,‘//*[@id="user_id"]/em[2]‘) test1=(By.XPATH,‘//*[@id="greeting"]/span‘) def username(self,username): self.findElement(*self.username_loc).send_keys(username) def password(self,password): self.findElement(*self.password_loc).send_keys(password) @property def clickLogin(self): self.findElement(*self.login_loc).click() t.sleep(3) def login(self,username,password): self.username(username) self.password(password) self.clickLogin @property def getDivText(self): t.sleep(2) return self.findElement(*self.divText).text @property def getDivText1(self): t.sleep(2) return self.findElement(*self.divText1).text @property def getDivText2(self): return self.findElement(*self.divText2).click @property def getNick(self): return self.findElement(*self.nick).text @property def getNick1(self): return self.findElement(*self.test1).text @property def getUrl(self): return self.driver.current_url
执行测试用例:(test下的)
import time as t from page.sina import Sina from page.init import InitSina from utils.joinUtils import readJson import unittest class QQTest(InitSina,Sina): def test_sina_click_005(self): """点击登陆成功1""" self.login( readJson()[‘login‘][‘username‘], readJson()[‘login‘][‘password‘]) self.assertEqual(self.getNick,readJson()[‘login‘][‘username‘]) def test_sina_click_006(self): """点击登陆成功2""" self.login( readJson()[‘login‘][‘username‘], readJson()[‘login‘][‘password‘]) self.assertEqual(self.getNick1,readJson()[‘login‘][‘text‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
3、错误的断言
from selenium.webdriver.common.by import By from base.base import WebDriver import time as t class Sina(WebDriver): username_loc=(By.ID,‘freename‘) password_loc=(By.ID,‘freepassword‘) login_loc=(By.LINK_TEXT,‘登录‘) divText=(By.XPATH,‘/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]‘) divText1=(By.XPATH,‘/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[2]‘) divText2=(By.LINK_TEXT,‘注册‘) nick=(By.XPATH,‘//*[@id="user_id"]/em[2]‘) test1=(By.XPATH,‘//*[@id="greeting"]/span‘) def username(self,username): self.findElement(*self.username_loc).send_keys(username) def password(self,password): self.findElement(*self.password_loc).send_keys(password) @property def clickLogin(self): self.findElement(*self.login_loc).click() t.sleep(3) def login(self,username,password): self.username(username) self.password(password) self.clickLogin @property def getDivText(self): t.sleep(2) return self.findElement(*self.divText).text @property def getDivText1(self): t.sleep(2) return self.findElement(*self.divText1).text @property def getDivText2(self): return self.findElement(*self.divText2).click @property def getNick(self): return self.findElement(*self.nick).text @property def getNick1(self): return self.findElement(*self.test1).text
2、执行测试用例:
import time as t from page.sina import Sina from page.init import InitSina from utils.joinUtils import readJson import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆验证""" self.login(username=‘‘,password=‘‘) self.clickLogin self.assertEqual(self.getDivText,readJson()[‘login‘][‘notEamil‘]) def test_sina_login_002(self): """登陆,验证邮箱格式不正确""" self.login(username=‘dsa‘, password=‘dasd‘) self.clickLogin self.assertEqual(self.getDivText,readJson()[‘login‘][‘formatEamil‘]) def test_sina_login_003(self): """登陆,验证用户名和密码错误""" self.login(username=‘dsa‘, password=‘dasd‘) self.clickLogin self.assertEqual(self.getDivText,readJson()[‘login‘][‘errorEmail‘]) def test_sina_login_004(self): self.login(username=‘dasdsa@sina.com‘,password=‘‘) self.clickLogin self.assertEqual(self.getDivText1,readJson[‘login‘][‘shum if __name__ == ‘__main__‘: unittest.main(verbosity=2)
二、数据驱动
把错误信息分离出去
1、在data下建一个json文件
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)
3、打开文件
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())
输出结果:
4、执行用例
import time as t from page.sina import Sina from page.init import InitSina from utils.joinUtils import readJson import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆验证""" self.login(username=‘‘,password=‘‘) self.clickLogin self.assertEqual(self.getDivText,readJson()[‘login‘][‘notEamil‘]) def test_sina_login_002(self): """登陆,验证邮箱格式不正确""" self.login(username=‘dsa‘, password=‘dasd‘) self.clickLogin self.assertEqual(self.getDivText,readJson()[‘login‘][‘formatEamil‘])
if __name__ == ‘__main__‘:
unittest.main(verbosity=2)
编码问题:
解决办法
1、设置IDE的编码为UTF-8
2、读取文件的时候,设置编码
三、Python操作Yaml文件
安装yali库:
1、在data下创建一个yaml文件,输入:
2、获取文件的路径(导入os,获取路径)
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)
3、读取文件的内容,(导入上一个模块的路径,读取yaml文件里面的内容)
import yaml from utils.pathUtils import filePath def readYaml(): """读取yaml文件里面的内容""" return yaml.load(open(filePath(fileName=‘sina.yaml‘),encoding=‘utf-8‘)) # print(readYaml())
执行以上代码,结果为:
4、执行测试用例(先导入上一个模块的参数,修改之前的断言路径)
import time as t from page.sina import Sina from page.init import InitSina # from utils.joinUtils import readJson from utils.yamlUtils import readYaml import unittest class QQTest(InitSina,Sina): def test_sina_login_001(self): """登陆验证""" self.login(username=‘‘,password=‘‘) self.clickLogin self.assertEqual(self.getDivText,readYaml()[‘login‘][‘notEamil‘]) if __name__ == ‘__main__‘: unittest.main(verbosity=2)
执行以上代码,结果为:
四、把网址分离出去
1、创建一个yaml文件,输入网址信息
2、获取文件的路径(内容)
import os def getUrl(): return yaml.load(open(filePath(directory=‘confug‘,fileName=‘url.yaml‘)))[‘url‘][‘qa‘] #print(getUrl())
执行以上代码,结果为:
3、在init里获取上一个文件的参数,把网址修改为
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测试框架
下载:库:pip3 install pytest
1、查看信息
def add(a,b): return a+b def test_add_001(): assert add(1,1)==2 def test_add_002(): assert add(‘hi‘,‘!‘)==‘hi!‘
进入该文件的目录,获取文件信息(用cd进入)
输入:python3 -m pytest -v test_add.py查看详细信息