python 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)

5、测试套件

5.1指定运行一个模块中的一个测试

5.2 指定按顺序运行一个模块中的多个测试

5.3 自动发现多个测试

6、测试报告

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值