测试人社区

20201112_Pytest 自动化测试框架

参考资料

pytest 安装

pip install pytest

运行的命令

pytest 
pytest 文件名
  • -v 打印详细结果
  • -x 遇到错误停止测试
  • –maxfail=num 可以错误的最大数值,达到了就退出测试

pytest 插件

pytest-assume

pip install pytest-assume
# 加法函数
import pytest


def add(x, y):
    return x + y


# 测试用例1
def test_add():
    print("我是测试用例1")
    assert add(1, 2) == 3


# 测试用例2
def test_add2():
    print("我是测试用例2")
    assert add(1.2, 3.1) == 3.3
    assert add(3, 2) == 5
    print("测试用例2执行完毕")

def test_add3():
    print("我是测试用例3")
    pytest.assume(add(1.2, 3.1) == 3.3)
    pytest.assume(add(3, 2) == 5)
    print("测试用例3执行完毕")

pytest-rerunfailures

pip install pytest-rerunfailures

代码

import random

def add(x, y):
    return x + y

# 测试用例1
def test_add():
    print("我是测试用例1")
    assert add(1, 2) == 3


# 测试用例2
def test_add2():
    random_num = random.randint(2,5)
    print(f"random_num的值为{random_num}")
    assert random_num == 3

运行命令

pytest --reruns 3 -v test_rerun.py

pytest-ordering

pip install pytest-ordering

代码

import time

import pytest

value = 0

@pytest.mark.run(order=10)
def test_case1():
    print('我是测试用例1')
    time.sleep(2)
    assert value == 10

@pytest.mark.run(order=8)
def test_case2():
    global value
    value = 10
    assert value == 10

pytest-sugar

pip install pytest-sugar

高阶用法

参数化

import pytest

@pytest.mark.parametrize(
    "x,y",
    [
        (3, 3),
        (2+4, 6),
        (6*2, 11)
    ]
)
def test_equal(x, y):
    assert x == y

自定义 marks

pytest.ini

[pytest]
markers =
    webtest: webtest
    hello: hello case
    P0: P0 case
    P1: P1 case

代码

import random
import time

import pytest

count = random.randint(1,5)

@pytest.mark.P0
@pytest.mark.hello
class TestMark:

    @pytest.mark.webtest
    def test_case1(self):
        print('test_case1')
        time.sleep(count)
        assert count == 3

    @pytest.mark.P1
    def test_case2(self):
        print('test_case2')
        time.sleep(count)
        assert count == 4

    def test_case3(self):
        print('test_case3')
        time.sleep(count)
        assert count == 5

运行命令

pytest -m "P0 and not webtest" test_mark.py

fixtures

import pytest

@pytest.fixture()
def login_and_logout():
    print('执行登录操作\n')
    yield
    print('执行注销操作')


class TestDemo:

    def test_case1(self, login_and_logout):
        print('test_case1')
        assert 1 == 1

    def test_case2(self, login_and_logout):
        print('test_case2')
        assert 2 == 2


class TestDemo2:

    @pytest.mark.usefixtures('login_and_logout')
    def test_case1(self):
        print('test_case1')
        assert 1 == 1

    def test_case2(self):
        print('test_case2')
        assert 2 == 2

autouse

import pytest

@pytest.fixture(scope='module', autouse=True)
def login_and_logout():
    print('执行登录操作\n')
    yield
    print('执行注销操作')

@pytest.fixture(scope='class', autouse=True)
def click_home():
    print('点击home按钮\n')
    yield
    print('点击结束')


class TestDemo:

    def test_case1(self):
        print('test_case1')
        assert 1 == 1

    def test_case2(self):
        print('test_case2')
        assert 2 == 2


class TestDemo2:

    def test_case1(self):
        print('test_case1')
        assert 1 == 1

    def test_case2(self):
        print('test_case2')
        assert 2 == 2

onftest.py

import pytest

@pytest.fixture(scope='session', autouse=True)
def build_db_connection():
    print('建立数据库连接')
    yield
    print('关闭数据库连接')

allure

pip install allure-pytest

模块和步骤定义

import allure

@allure.feature('测试allure的功能')
class TestAllure:

    @allure.story('第一个测试用例')
    def test_case1(self):
        with allure.step('test_case1 的第一步'):
            assert 1 == 1
        with allure.step('test_case1 的第二步'):
            assert 1 == 2

    @allure.story('第二个测试用例')
    def test_case2(self):
        with allure.step('test_case2 的第一步'):
            assert 1 == 1

运行测试用例并收集结果

pytest --alluredir=testreport/xml .

生成html报告

allure generate --clean alluredir testreport/xml -o testreport/html