前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >软件测试|Pytest的必会技巧(一)

软件测试|Pytest的必会技巧(一)

原创
作者头像
霍格沃兹测试开发Muller老师
发布2023-02-08 18:30:36
2770
发布2023-02-08 18:30:36
举报

pytest setup和teardown

我们在使用selenium执行web自动化测试的时候,当我们需要执行多条测试用例时,执行一条用例就启动一次浏览器显然效率就太低了,我们需要一次启动浏览器,执行多条用例。pytest可以满足我们的需求吗?答案是pytest的setup和teardown完美匹配我们的需求,unittest有的它有,unittest没有的它也有!

作用范围

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

函数级

setup_function/teardown_function

每个用例开始和结束调用一次

代码语言:python
复制
# test_demo.py

# coding:utf-8
import pytest
# 函数式

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_fixt.py"])

运行结果:

代码语言:python
复制
============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_one setup_function:每个用例开始前都会执行
PASSED                                            [ 33%]正在执行----test_one
teardown_function:每个用例结束后都会执行

test_demo.py::test_two setup_function:每个用例开始前都会执行
FAILED                                            [ 66%]正在执行----test_two

test_demo.py:19 (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_demo.py:23: AssertionError

teardown_function:每个用例结束后都会执行

test_demo.py::test_three setup_function:每个用例开始前都会执行
PASSED                                          [100%]正在执行----test_three
teardown_function:每个用例结束后都会执行


========================= 1 failed, 2 passed in 0.04s =========================

从结果可以看出用例执行顺序:setup_function》用例1》teardown_function, setup_function》用例2》teardown_function, setup_function》用例3》teardown_function

注:加上-s参数是为了显示用例的打印信息,便于查看。

setup_module/teardown_module

setup_module是所有用例开始前只执行一次,teardown_module是所有用例结束后只执行一次

代码语言:python
复制
import pytest
# 函数式
def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("比如:所有用例开始前只打开一次浏览器")

def teardown_module():
    print("teardown_module:整个.py模块只执行一次")
    print("比如:所有用例结束只最后关闭浏览器")

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_demo.py"])
    

从运行结果可以看到setup_module和teardown_module只执行了一次

代码语言:python
复制
============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_one setup_module:整个.py模块只执行一次
比如:所有用例开始前只打开一次浏览器
setup_function:每个用例开始前都会执行
PASSED                                            [ 33%]正在执行----test_one
teardown_function:每个用例结束前都会执行

test_demo.py::test_two setup_function:每个用例开始前都会执行
FAILED                                            [ 66%]正在执行----test_two

test_demo.py:27 (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_demo.py:31: AssertionError

teardown_function:每个用例结束前都会执行

test_demo.py::test_three setup_function:每个用例开始前都会执行
PASSED                                          [100%]正在执行----test_three
teardown_function:每个用例结束前都会执行
teardown_module:整个.py模块只执行一次



========================= 1 failed, 2 passed in 0.04s =========================

注:setup_function/teardown_function和setup_module/teardown_module这四种方法是可以任意组合的,用一个和多个都可以

类和方法

setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

代码语言:python
复制
# coding:utf-8
#test_fixtclass.py

# coding:utf-8
import pytest
# 类和方法
class TestCase():

    def setup(self):
        print("setup: 每个用例开始前执行")

    def teardown(self):
        print("teardown: 每个用例结束后执行")

    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_demo.py"])

运行结果

代码语言:python
复制
test_fixtclass.py setup_class:所有用例执行之前
setup_method:  每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_one
.teardown: 每个用例结束后执行
teardown_method:  每个用例结束后执行
setup_method:  每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_two
Fteardown: 每个用例结束后执行
teardown_method:  每个用例结束后执行
setup_method:  每个用例开始前执行
setup: 每个用例开始前执行
正在执行----test_three
.teardown: 每个用例结束后执行
teardown_method:  每个用例结束后执行
teardown_class:所有用例执行之前

从结果看出,运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可。

函数和类混合

如果一个.py的文件里面既有函数用例又有类和方法用例,运行顺序又是怎样的呢?

代码语言:python
复制
# coding:utf-8
import pytest
# 类和方法

def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("比如:所有用例开始前只打开一次浏览器")

def teardown_module():
    print("teardown_module:整个.py模块只执行一次")
    print("比如:所有用例结束只最后关闭浏览器")

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')

class TestCase():

    def setup_class(self):
        print("setup_class:所有用例执行之前")

    def teardown_class(self):
        print("teardown_class:所有用例执行之前")

    def test_three(self):
        print("正在执行----test_three")
        x = "this"
        assert 'h' in x

    def test_four(self):
        print("正在执行----test_four")
        x = "hello"
        assert hasattr(x, 'check')

if __name__ == "__main__":
    pytest.main(["-s", "test_fixtclass.py"])

运行结果:

代码语言:python
复制
test_fixtclass.py setup_module:整个.py模块只执行一次
比如:所有用例开始前只打开一次浏览器
setup_function:每个用例开始前都会执行
正在执行----test_one
.teardown_function:每个用例结束前都会执行
setup_function:每个用例开始前都会执行
正在执行----test_two
Fteardown_function:每个用例结束前都会执行
setup_class:所有用例执行之前
正在执行----test_three
.正在执行----test_four
Fteardown_class:所有用例执行之前
teardown_module:整个.py模块只执行一次

从运行结果看出,setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • pytest setup和teardown
    • 作用范围
      • 函数级
        • setup_module/teardown_module
          • 类和方法
            • 函数和类混合
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
            http://www.vxiaotou.com