zoukankan      html  css  js  c++  java
  • pytest框架(四)

    测试用例setup和teardown

    代码示例一

     1 # coding=utf-8
     2 import pytest
     3 
     4 
     5 def setup_module():
     6     print("setup_module:整个.py模块只执行一次")
     7     print("比如:所有用例开始前只打开一次浏览器")
     8 
     9 
    10 def teardown_module():
    11     print("teardown_module:整个.py模块只执行一次")
    12     print("比如:所有用例结束只最后关闭浏览器")
    13 
    14 
    15 def setup_function():
    16     print("setup_function:每个用例开始前都会执行")
    17 
    18 
    19 def teardown_function():
    20     print("teardown_function:每个用例结束前都会执行")
    21 
    22 
    23 def test_one():
    24     print("正在执行----test_one")
    25     x = "this"
    26     assert 'h' in x
    27 
    28 
    29 def test_two():
    30     print("正在执行----test_two")
    31     x = "hello"
    32     assert hasattr(x, 'check')
    33 
    34 
    35 def test_three():
    36     print("正在执行----test_three")
    37     a = "hello"
    38     b = "hello world"
    39     assert a in b
    40 
    41 
    42 if __name__ == "__main__":
    43     pytest.main(["-s", "test_fixt.py"])

    代码示例二

     1 # coding=utf-8
     2 
     3 import pytest
     4 
     5 
     6 class TestClass:
     7     def setup(self):
     8         print("setup: 每个用例开始前执行")
     9 
    10     def teardown(self):
    11         print("teardown: 每个用例结束后执行")
    12 
    13     def setup_class(self):
    14         print("setup_class:所有用例执行之前")
    15 
    16     def teardown_class(self):
    17         print("teardown_class:所有用例执行之前")
    18 
    19     def setup_method(self):
    20         print("setup_method:  每个用例开始前执行")
    21 
    22     def teardown_method(self):
    23         print("teardown_method:  每个用例结束后执行")
    24 
    25     def test_one(self):
    26         x = "this"
    27         assert 'h' in x
    28 
    29     def test_two(self):
    30         x = "hello"
    31         assert hasattr(x, 'check')
    32 
    33     def test_three(self):
    34         a = "hello"
    35         b = "hello world"
    36         assert a in b
    37 
    38 
    39 if __name__ == "__main__":
    40     pytest.main(['-q', 'test_class.py'])
  • 相关阅读:
    jQuery基础之让出$,与其他库共存
    什么是闭包
    绑定repeater时三目运算加特殊结果处理
    将同一张表出来的两部分内容再合成一张表
    后台往前台写弹窗代码不显示
    固定行列转换加分段统计
    js调用后台方法(如果你能容忍执行的后台方法变成一个常量)
    javascript遍历数组
    基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制(上篇)
    echarts
  • 原文地址:https://www.cnblogs.com/loveapple/p/9519859.html
Copyright © 2011-2022 走看看