美文网首页
Pytest-3运行级别

Pytest-3运行级别

作者: iscona | 来源:发表于2023-08-10 15:19 被阅读0次

1.执行级别
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)

setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

作者:sofiiii
链接:https://www.jianshu.com/p/8891a8435ede
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2.函数

setup_function/teardown_function
每个用例(函数)开始和结束时调用一次
#!/usr/bin/env python
# -*- coding:utf8 -*-

import pytest

def setup_module():
    print("[setup_module]:整个.py模块开始时执行一次")

def teardown_module():
    print("[setup_module]:整个.py模块结束时执行一次")

def setup_function():
    print("[setup_function]: 每个用例(函数)开始时调用一次")

def teardown_function():
    print("[teardown_function]: 每个用例(函数)结束时调用一次")

def test_one():
    print("[test_one]:正在执行")
    x = "this"
    assert 'h' in x

def test_two():
    print("[test_two]:正在执行")
    x = "hello"
    assert hasattr(x, 'check')

def test_three():
    print("[test_three]:正在执行")
    a = "hello"
    b = "hello world"
    assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_api.py"])
➜  testcases pytest -s
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 3 items

test_api.py [setup_module]:整个.py模块开始时执行一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_one]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_two]:正在执行
F[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_three]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_module]:整个.py模块结束时执行一次

========================FAILURES ========================
______________________test_two ______________________

    def test_two():
        print("[test_two]:正在执行")
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_api.py:26: AssertionError
===================== short test summary info =====================
FAILED test_api.py::test_two - AssertionError: assert False
===================== 1 failed, 2 passed in 0.11s =====================

2.类

#!/usr/bin/env python
# -*- coding:utf8 -*-

import pytest

class TestCase():

    def setup_class(self):
        print("[setup_class]:类的所有用例执行前执行")

    def teardown_class(self):
        print("[teardown_class]:类的所有用例执行后执行")

    def setup_method(self):
        print("[setup_method]:每个用例执行前执行")

    def teardown_method(self):
        print("[teardown_method]:每个用例执行后执行")

    def test_one(self):
        print("[test_one]:正在执行")
        x = "this"
        assert 'h' in x

    def test_two(self):
        print("[test_two]:正在执行")
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        print("[test_three]:正在执行")
        a = "hello"
        b = "hello world"
        assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_api.py"])
➜  testcases pytest -s
============================================================================== test session starts ===============================================================================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 3 items

test_api.py [setup_class]:类的所有用例执行前执行
[setup_method]:每个用例执行前执行
[test_one]:正在执行
.[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_two]:正在执行
F[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_three]:正在执行
.[teardown_method]:每个用例执行后执行
[teardown_class]:类的所有用例执行后执行


=========================FAILURES =========================
__________________________ TestCase.test_two __________________________

self = <testcases.test_api.TestCase object at 0x101bd6160>

    def test_two(self):
        print("[test_two]:正在执行")
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_api.py:28: AssertionError
=======================short test summary info =======================
FAILED test_api.py::TestCase::test_two - AssertionError: assert False
======================= 1 failed, 2 passed in 0.09s =======================

3.函数+类

#!/usr/bin/env python
# -*- coding:utf8 -*-

import pytest

def setup_module():
    print("[setup_module]:整个.py模块开始时执行一次")

def teardown_module():
    print("[setup_module]:整个.py模块结束时执行一次")

def setup_function():
    print("[setup_function]: 每个用例(函数)开始时调用一次")

def teardown_function():
    print("[teardown_function]: 每个用例(函数)结束时调用一次")


def test_four():
    print("[test_four]:正在执行")
    x = "this"
    assert 'h' in x


def test_five():
    print("[test_five]:正在执行")
    x = "hello"
    assert hasattr(x, 'check')


def test_six():
    print("[test_six]:正在执行")
    a = "hello"
    b = "hello world"
    assert a in b

class TestCase():

    def setup_class(self):
        print("[setup_class]:类的所有用例执行前执行")

    def teardown_class(self):
        print("[teardown_class]:类的所有用例执行后执行")

    def setup_method(self):
        print("[setup_method]:每个用例执行前执行")

    def teardown_method(self):
        print("[teardown_method]:每个用例执行后执行")

    def test_one(self):
        print("[test_one]:正在执行")
        x = "this"
        assert 'h' in x

    def test_two(self):
        print("[test_two]:正在执行")
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        print("[test_three]:正在执行")
        a = "hello"
        b = "hello world"
        assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_api.py"])
➜  testcases pytest -s
=================== test session starts ===================
platform darwin -- Python 3.7.2, pytest-7.4.0, pluggy-1.2.0
rootdir: /Users/qina/workspace/python-space/python-test/testcases
collected 6 items

test_api.py [setup_module]:整个.py模块开始时执行一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_four]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_five]:正在执行
F[teardown_function]: 每个用例(函数)结束时调用一次
[setup_function]: 每个用例(函数)开始时调用一次
[test_six]:正在执行
.[teardown_function]: 每个用例(函数)结束时调用一次
[setup_class]:类的所有用例执行前执行
[setup_method]:每个用例执行前执行
[test_one]:正在执行
.[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_two]:正在执行
F[teardown_method]:每个用例执行后执行
[setup_method]:每个用例执行前执行
[test_three]:正在执行
.[teardown_method]:每个用例执行后执行
[teardown_class]:类的所有用例执行后执行
[setup_module]:整个.py模块结束时执行一次


======================== FAILURES ========================
_________________________ test_five _________________________

    def test_five():
        print("[test_five]:正在执行")
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_api.py:28: AssertionError
_________________________ TestCase.test_two _________________________

self = <testcases.test_api.TestCase object at 0x104702f60>

    def test_two(self):
        print("[test_two]:正在执行")
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_api.py:59: AssertionError
========================= short test summary info =========================
FAILED test_api.py::test_five - AssertionError: assert False
FAILED test_api.py::TestCase::test_two - AssertionError: assert False
========================= 2 failed, 4 passed in 0.12s =========================

相关文章

  • 启动管理

    CentOS 6.x启动管理 系统运行级别 1、运行级别 2、运行级别 runlevel #查看运行级别命令 in...

  • 运行级别(centos7新特征)(未完成)

    运行级别image.png 设置运行级别语法: systemctl [选项] [运行级别]选项:get-defau...

  • Linux 服务管理

    视频教程:Linux服务管理 一、系统运行级别 查看运行级别命令 runlevel修改运行级别命令 init +[...

  • 第十五章启动管理

    15.1 CentOS 6.x启动管理 15.1.1 系统运行级别 1、 运行级别 2、 运行级别命令 [root...

  • 如何查看系统的信息?

    1.如何查看系统运行级别 临时修改运行级别 永久修改运行级别vi /etc/inittab/ 2.查看CPU 3....

  • 常用命令

    常用命令(1) -- 运行级别 常用的运行级别为3和5,要修改默认的运行级别,修改 /etc/inittab 文件...

  • Linux系统有7个运行级别(runlevel)

    Linux系统有7个运行级别(runlevel)运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常...

  • Linux学习笔记(11) -- 运行级别

    本文以CentOS7为例。 本文目录结构 1 运行级别介绍 在Linux系统分为以下7个运行级别。 常用运行级别为...

  • 系统运行级别

  • linux 运行级别

    0 关机1 单用户模式2 无NFS 多用户模式3 文本模式4 无用5 图形模式6 重启

网友评论

      本文标题:Pytest-3运行级别

      本文链接:https://www.haomeiwen.com/subject/wdtapdtx.html