unittest测试框架
1、导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
-
unittest是系统自带的,无需安装
-
需要安装以下包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium --target E:\pyprojects\venv\Lib\site-packages
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple urllib3 --target E:\pyprojects\venv\Lib\site-packages
2、创建类
2.1 全局变量
driver = None
-
定义驱动器
-
放在模块中,不要放在类中
2.2 创建测试类
class class_name(unittest.TestCase):
- 括号内表示继承unittest.TestCase类
- 大小写不能弄错
2.3 创建初始化和还原环境的函数
@classmethod
def setUpClass(cls):
print("Run before all tests start")
# 必须使用@classmethod装饰器
# cls不能省略
@classmethod
def tearDownClass(cls):
print("Run after all tests done")
# 必须使用@classmethod装饰器
# cls不能省略
def setUp(self):
print("Run at the begin of each test")
# self 不能省略
def tearDown(self):
print("Run at the end of each test")
# self 不能省略
2.4 测试函数
def test_A(self):
print("A")
def test_B(self):
print("B")
2.5 运行测试
if __name__=="__main__":
unittest.main(verbosity=2)
2.6 示例
2.6.1 示例一
代码:
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
class class_name(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("Run before all tests start")
# 必须使用@classmethod装饰器
# cls不能省略
@classmethod
def tearDownClass(cls):
print("Run after all tests done")
# 必须使用@classmethod装饰器
# cls不能省略
def setUp(self):
print("Run at the begin of each test")
# self 不能省略
def tearDown(self):
print("Run at the end of each test")
# self 不能省略
def test_A(self):
print("A")
def test_B(self):
print("B")
if __name__=="__main__":
unittest.main(verbosity=2)
结果:
(venv) PS E:\pyprojects> & e:/pyprojects/venv/Scripts/python.exe e:/pyprojects/test.py
Run before all tests start
test_A (__main__.class_name) ... Run at the begin of each test
A
Run at the end of each test
ok
test_B (__main__.class_name) ... Run at the begin of each test
B
Run at the end of each test
ok
Run after all tests done
2.6.2 示例二
代码:
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
driver = None
#初始化变驱动器
class class_name(unittest.TestCase):
@classmethod
def setUpClass(cls):
global driver
driver = webdriver.Chrome(r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
@classmethod
def tearDownClass(cls):
driver.quit()
def setUp(self):
print("Run at the begin of each test")
def tearDown(self):
print("Run at the end of each test")
def test_A(self):
print("A")
def test_B(self):
print("B")
if __name__=="__main__":
unittest.main(verbosity=2)
结果:
PS E:\pyprojects> & e:/pyprojects/venv/Scripts/activate.ps1
(venv) PS E:\pyprojects> & e:/pyprojects/venv/Scripts/python.exe e:/pyprojects/test.py
DevTools listening on ws://127.0.0.1:57082/devtools/browser/6785f7e7-2dbd-4383-84f2-c063a59d27ac
test_A (__main__.class_name) ... Run at the begin of each test
A
Run at the end of each test
ok
test_B (__main__.class_name) ... Run at the begin of each test
B
Run at the end of each test
ok
----------------------------------------------------------------------
Ran 2 tests in 7.223s
OK
3、断言
4、参数化
4.1 安装nose_parameterized
pip install nose_parameterized
4.2 导入包parameterized
import parameterized
4.3 定义参数数据
data = [[data11, data12],
[data21, data22]
]
- 参数放在列表中
4.4 引入参数
@parameterized.parameterized.expand(data)
def test...(self, param_1, param_2)