pytest小demo

环境

  • ubuntu16.04
  • python3.7
  • pytest5.4.3

样例1

test_demo1.py 内容

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2020/7/8 上午11:12

import pytest


def setup_module():
    print("\n-------------- setup_module --------------")


def teardown_module():
    print("\n-------------- teardown_module --------------")


def setup_function():
    print("\n-------------- setup_function --------------")


def teardown_function():
    print("\n-------------- teardown_function --------------")


def test_no_class_func1():
    print("\n正在执行test_no_class_func1")
    assert 1 + 2 == 3


def test_no_class_func2():
    print("\n正在执行test_no_class_func2")
    assert 1 + 2 == 4


class TestCase(object):
    def setup(self):
        print("\n-------------- TestCase.setup --------------")

    def teardown(self):
        print("\n-------------- TestCase.teardown --------------")

    def setup_method(self):
        print("\n-------------- TestCase.setup_method --------------")

    def teardown_method(self):
        print("\n-------------- TestCase.teardown_method --------------")

    def setup_class(self):
        print("\n-------------- TestCase.setup_class --------------")

    def teardown_class(self):
        print("\n-------------- TestCase.teardown_class --------------")

    def test_one(self):
        print("\n正在执行----test_one")
        x = "he"
        assert 'h' in x

    def test_two(self):
        print("\n正在执行test_two")
        assert "she" not in "she is a girl"

    def test_three(self):
        print("\n正在执行test_tree")
        assert self.add(1, 2) == 3

    def add(self,a, b):
        print("\n这是加减法")
        return a + b


结果

...
============================= test session starts ==============================
...
collecting ... collected 5 items

test_demo1.py::test_no_class_func1 
-------------- setup_module --------------

-------------- setup_function --------------
PASSED                                [ 20%]
正在执行test_no_class_func1

-------------- teardown_function --------------

test_demo1.py::test_no_class_func2 
-------------- setup_function --------------
FAILED                                [ 40%]
正在执行test_no_class_func2

test_demo1.py:45 (test_no_class_func2)
3 != 4

Expected :4
Actual   :3
<Click to see difference>

def test_no_class_func2():
        print("\n正在执行test_no_class_func2")
>       assert 1 + 2 == 4
E       assert 3 == 4

test_demo1.py:48: AssertionError

-------------- teardown_function --------------

test_demo1.py::TestCase::test_one 
-------------- TestCase.setup_class --------------

-------------- TestCase.setup_method --------------

-------------- TestCase.setup --------------
PASSED                                 [ 60%]
正在执行----test_one

-------------- TestCase.teardown --------------

-------------- TestCase.teardown_method --------------

test_demo1.py::TestCase::test_two 
-------------- TestCase.setup_method --------------

-------------- TestCase.setup --------------
FAILED                                 [ 80%]
正在执行test_two

test_demo1.py:74 (TestCase.test_two)
self = <test_rrrcpy.src.test_demo1.TestCase object at 0x7ff048c735d0>

    def test_two(self):
        print("\n正在执行test_two")
>       assert "she" not in "she is a girl"
E       AssertionError: assert 'she' not in 'she is a girl'
E         'she' is contained here:
E           she is a girl
E         ? +++

test_demo1.py:77: AssertionError

-------------- TestCase.teardown --------------

-------------- TestCase.teardown_method --------------

test_demo1.py::TestCase::test_three 
-------------- TestCase.setup_method --------------

-------------- TestCase.setup --------------
PASSED                               [100%]
正在执行test_tree

这是加减法

-------------- TestCase.teardown --------------

-------------- TestCase.teardown_method --------------

-------------- TestCase.teardown_class --------------

-------------- teardown_module --------------


=================================== FAILURES ===================================
_____________________________ test_no_class_func2 ______________________________

    def test_no_class_func2():
        print("\n正在执行test_no_class_func2")
>       assert 1 + 2 == 4
E       assert 3 == 4

test_demo1.py:48: AssertionError
---------------------------- Captured stdout setup -----------------------------

-------------- setup_function --------------
----------------------------- Captured stdout call -----------------------------

正在执行test_no_class_func2
--------------------------- Captured stdout teardown ---------------------------

-------------- teardown_function --------------
______________________________ TestCase.test_two _______________________________

self = <test_rrrcpy.src.test_demo1.TestCase object at 0x7ff048c735d0>

    def test_two(self):
        print("\n正在执行test_two")
>       assert "she" not in "she is a girl"
E       AssertionError: assert 'she' not in 'she is a girl'
E         'she' is contained here:
E           she is a girl
E         ? +++

test_demo1.py:77: AssertionError
---------------------------- Captured stdout setup -----------------------------

-------------- TestCase.setup_method --------------

-------------- TestCase.setup --------------
----------------------------- Captured stdout call -----------------------------

正在执行test_two
--------------------------- Captured stdout teardown ---------------------------

-------------- TestCase.teardown --------------

-------------- TestCase.teardown_method --------------
=========================== short test summary info ============================
FAILED test_demo1.py::test_no_class_func2 - assert 3 == 4
FAILED test_demo1.py::TestCase::test_two - AssertionError: assert 'she' not i...
========================= 2 failed, 3 passed in 0.20s ==========================

Process finished with exit code 1

Assertion failed

Assertion failed

Assertion failed


总结

  • 模块级(setup_module/teardown_module):在模块起止生效
  • 函数级(setup_function/teardown_function):在非类方法的函数起止生效
  • 执行顺序是:
    setup_model->
    1. setup_class->setup_method-> setup ->用例class func -> teardown-> teardown_method-> teardown_class-> teardown_model
    2. setup_function ->用例no class func -> teardown_function-> teardown_model
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值