pytest用法小结

pytest用法小结

pytest最常用法总结,当然不止这一点功能。关于更多更强大的插件,可以根据自己需要来定制。

安装 #

pytest 安装和使用都非常简单, 只需pip install pytest

编写测试代码 #

使用pytest,不需要像unittest模块一样,pytest使用的是python自带的assert,如:

def test_global_function():
    assert 1 == 1

使用pytest.mark #

pytest.mark 用于给测试方法打上标签,在稍后的执行中会讲到如何使用marker

@pytest.mark.marker_self
def test_global_function():
    assert 1 == 1

使用pytest.fixture #

@pytest.fixture
def google_url():
    return "http://google.com"

setup 和 teardown #

setupteardown方法作用范围,分为全局作用,类作用,方法作用

  • 全局:作用于全局测试函数
  • 类: 作用于自身类
  • 类方法: 作用于类函数

简单举例: #

# 全局
def setup_function(function):
    print("setup function global")

def teardown_function(function):
    print("teardown function global")

# 类
class Test_fixture:

    @classmethod
    def setup_class(cls):
        print("class setup method")

    @classmethod
    def teardown_class(cls):
        print("class teardown method")

# 类方法
    def setup_method(self, method):
        print("class method setup function")

    def teardown_method(self, method):
        print("class method teardown function")

pytest配置文件 #

配置文件名为pytest.ini setup.cfg tox.ini 关于配置文件优先级请查阅官方文档 简单举例:

[pytest]
addopts = --maxfail=2 // set default pytest options
python_classes = *Api //execute all *Api like TestSingUpApi or TestSignUpApi

更多配置内容参见https://docs.pytest.org/en/latest/customize.html

执行方法 #

此处只列举了较常用的参数,主要用于演示marker和选中,和文件,类,函数的选中

# 执行方法:pytest [options] test_file::Test_class::test_method [plugin-options]

# 执行有标记为marker_self的case
pytest -v -m "marker_self" test_demo.py::Test_fixture::test_fixture_demo

# 执行有标记为marker_self 且 marker_other与的case
pytest -v -m "marker_self and marker_other" test_demo.py::Test_fixture

# 执行有标记为marker_self 或 marker_other的case
pytest -v -m "yeqiag or marker_other" test_demo.py

# 执行测试文件中某一个测试类的一个测试case
pytest -v test_demo.py::Test_fixture::test_fixture_demo

# 执行测试文件中某一个测试类
pytest -v test_demo.py::Test_fixture

# 执行测试文件
pytest -v test_demo.py

全部代码 #

"""
1.setup teardown 作用范围:
    全局的 作用于 全局的单个函数,
    class 作用于 该类的单个函数
2.mark的使用
    使用全局的skip, xfail来标记并略过一部分的测试case
    使用自定义的marker来区分测试等级
3.fixture的作用范围
    全局作用
    class内部作用
"""

import pytest


def setup_function(function):
    print("setup function global")

def teardown_function(function):
    print("teardown function global")

def test_simple_method():
    assert 1 == 1

@pytest.mark.marker_self
def test_global_function():
    assert 1 == 1

class Test_class_case:
    """
        Must Know These Things
        1. 测试类 init函数不会被执行
        2. 测试类 属性只读
    """
    @classmethod
    def setup_class(cls):
        print("class setup function")

    @classmethod
    def teardown_class(cls):
        print("class teardown function")

    def setup_method(self, method):
        print("class method setup function")

    def teardown_method(self, method):
        print("class method teardown function")

    @pytest.mark.xfail(reason="一定失败")
    def test_case0(self):
        assert 1 == 0

    def test_case1(self):
        assert 1 == 1

    @pytest.mark.skip(reason="我知道这是错的")
    def test_case2(self):
        assert 1 == 0

    def test_case3(self):
        assert True == False


@pytest.fixture
def google_url():
    return "http://google.com"

@pytest.mark.marker_self
def test_fixture_google(google_url):
    assert google_url == "http://google.com"
    assert 1 == 0

def test_(google_url):
    assert google_url == "http;//google.com"

def test_baidu_url(baidu_url):
    assert baidu_url == "http://baidu.com"

class Test_fixture:

    @classmethod
    def setup_class(cls):
        print("class setup method")

    @classmethod
    def teardown_class(cls):
        print("class teardown method")

    @pytest.fixture
    def baidu_url(self):
        return "http://www.baidu.com"

    @pytest.mark.marker_self
    def test_fixture_demo(self, baidu_url):
        assert baidu_url == "http://www.baidu.com"
        assert 1 == 0

    @pytest.mark.marker_other
    def test_fixture_demo_2(self, baidu_url):
        assert baidu_url == "http://google.com"

    @pytest.mark.marker_self
    @pytest.mark.skip(reason="没有原因三")
    def test_self_marker_demo0(self):
        assert 10 == 0

    @pytest.mark.marker_self
    def test_self_marker_demo1(self):
        assert 10 == 0

    @pytest.mark.marker_self
    @pytest.mark.marker_other
    def test_fixture_demo_google(self, google_url):
        assert google_url == "http://google.com"

执行结果截图

截图

未完待续…

访问量 访客数