Python+pytest知识点回顾

pip install pytest

pytest 单元测试框架
pytest高于unittest测试框架
unittest测试类需要继承unittest.TestCase类
pytest不需要继承,可以是一个函数,也可以是一个类

unittest参数化需要依赖第三方的库
pytest不需要依赖,直接使用内部的库parametrize

unittest测试报告HTMLTestRunner
pytest 测试报告pytest-html 或者是 allure

unittest是没有插件的
pytest有很丰富的插件

unittest不支持失败重试
pytest支持失败重试

import pytest

class TestF1:
    def test_01(self):
        print('AAA')

    def test02(self):
        print('BBB')

==:内容和类型必须同时满足
in:后者包含前者
is:前后者两个值相等

def A_test():
    assert 1==1

def B_test():
    assert 1==2

def test_in():
    assert '测试实战' in 'Python测试实战'

def test_is():
    assert 2 is 2

if __name__ == '__main__':
    pytest.main(['-v'])

执行pytest测试用例会显示进度条
pip install pytest-sugar 

1. -v:输出详细的信息
2. -s: 输出测试函数或者测试方法里面的print()的内容
3. -k:按分类执行测试点
4. -m:进行分组
5. -x:执行失败立即停止(后面的测试函数/方法不执行)
6. --maxfail:执行失败的最大次数(如果只有一个失败的--maxfail=1,后面的代码不执行,--maxfail=2执行后面的代码)
7. --tb=no:关闭信息
8. --tb=short:只输出assert的错误信息(会提示具体的函数以及错误代码)
9. --tb=line:一行行展示所有错误信息的位置(不会提示具体函数,但是会提示断言失败的位置)
10. --lf:定位错误
11. --ff:遇到错误继续执行
12. --duration=0:测试函数执行速度

pytest代码执行的顺序:
pytest -v xx.py
pytest -v xx.py::test_001
pytest -v -s xx.py
pytest -v -k 'login or logout' xx.py
pytest -v -m login xx.py
pytest -v -k 'login or ui' xx.py
pytest -v -x xx.py
pytest -v --maxfail=2 xx.py
pytest -v  xx.py --tb=no
pytest -v  xx.py --tb=short
pytest -v  xx.py --tb=line
pytest -v  xx.py --tb=line
pytest -v --lf xx.py
pytest -v --ff xx.py
pytest -v --duration=0 xx.py

@pytest.mark.api
def test_001():
    assert 1==1

class Login:
    @pytest.mark.login
    def test_login_01(self):
        print('登录成功1')

    @pytest.mark.login
    def test_login_02(self):
        print('登录成功2')

    @pytest.mark.logout
    def test_logout_01(self):
        print('退出登录1')

    @pytest.mark.logout
    def test_logout_02(self):
        print('退出登录2')


import requests

def test_baidu():
    '''测试百度链接'''
    r = requests.get('http://www.baidu.com')
    assert r.status_code == 200

def test_jd():
    '''测试京东链接'''
    r = requests.get('http://www.jd.com')
    assert r.status_code == 200

def test_taobao():
    '''测试淘宝链接'''
    r = requests.get('http://www.taobao.com')
    assert r.status_code == 200

def test_sina():
    '''测试新浪链接'''
    r = requests.get('https://mail.sina.com.cn/')
    assert r.status_code == 200


pytest 和 selenium 整合    
pip install pytest-selenium

执行:
pytest -v xx.py --driver Chrome
import time
from selenium import webdriver
driver = webdriver.Chrome()
import pytest

def login(selenium,username=None,password=None):
    '''测试公共分离'''
    selenium.get('https://mail.sina.com.cn/')
    time.sleep(2)
    selenium.find_element_by_id('freename').send_keys(username)
    time.sleep(2)
    selenium.find_element_by_id('freepassword').send_keys(password)
    selenium.find_element_by_class_name('loginBtn').click()

def test_login_pwd_null(selenium):
    '''测试新浪邮箱用户密码为空'''
    login(selenium=selenium,username='',password='')
    user = selenium.find_element_by_class_name('loginError.tip11').text
    assert user == '请输入邮箱名'

def test_pwd_null(selenium):
    '''测试新浪邮箱密码为空'''
    login(selenium=selenium,username='tao@sina.com',password='')
    pwd = selenium.find_element_by_class_name('loginError.tip13').text
    assert pwd == '请输入密码'
原文地址:https://www.cnblogs.com/Teachertao/p/12304019.html