• Selenium实战(八)——pytest单元测试(2)


    一、断言

      不像在unittest中的断言方法:assertEqual()、assertIn()、assertTrue()、assertIs()等。pytest单元测试框架并没有提供专门的断言方法,而是直接使用Python的assert进行断言。

      创建test_assert.py文件

     1 # 功能:用于计算a与b相加的和
     2 def add(a, b):
     3     return a + b
     4 
     5 
     6 # 功能:判断素数
     7 def is_prime(n):
     8     if n <= 1:
     9         return False
    10     for i in range(2, n):
    11         if n % i == 0:
    12             return False
    13     return True
    14 
    15 
    16 # 测试相等
    17 def test_add_1():
    18     assert add(3, 10) == 13
    19 
    20 
    21 # 测试不相等
    22 def test_add_2():
    23     assert add(4, 8) != 98
    24 
    25 
    26 # 测试大于或等于
    27 def test_add_3():
    28     assert add(9, 3) >= 11
    29 
    30 
    31 # 小于或等于
    32 def test_add_4():
    33     assert add(4, 7) <= 17
    34 
    35 
    36 # 测试包含
    37 def test_in():
    38     a = "hello"
    39     b = "ell"
    40     assert b in a
    41 
    42 
    43 # 测试不包含
    44 def test_not_in():
    45     a = "print"
    46     b = "OOO"
    47     assert b not in a
    48 
    49 
    50 # 判断是否为True
    51 def test_true_1():
    52      assert is_prime(13)
    53 
    54 
    55 # 判断是否为True
    56 def test_true_2():
    57     assert is_prime(7) is True
    58 
    59 
    60 # 判断是否不为True
    61 def test_true_3():
    62     assert not is_prime(4)
    63 
    64 
    65 # 判断是否不为True
    66 def test_true_4():
    67     assert is_prime(6) is not True
    68 
    69 
    70 # 判断是否为False
    71 def test_false_1():
    72     assert is_prime(8) is False

     二、Fixture

      Fixture通常用来对测试方法、测试函数、测试类和整个测试文件进行初始化或还原测试环境。创建test_fixtures_01.py文件:

     1 # 功能函数
     2 def multiply(a, b):
     3     return a * b
     4 
     5 
     6 # ============Fixture===========
     7 def setup_module(module):
     8     print("setup_module=============>")
     9 
    10 def teardown_module(module):
    11     print("teardown_module=================>")
    12 
    13 
    14 def setup_function(function):
    15     print("setup_function------------------>")
    16 
    17 def teardown_function(function):
    18     print("teardown_function----------->")
    19 
    20 def setup():
    21     print("setup...............>>")
    22 
    23 def teardown():
    24     print("teardown...........>>")
    25 
    26 # ==========测试用例==========
    27 def test_multiply_3_4():
    28     print('test_number_3_4')
    29     assert multiply(3, 4) == 12
    30 
    31 def test_multiply_a_3():
    32     print('test_strings_a_3')
    33     assert multiply('a', 3) == 'aaa'

       这里主要用到模块级别和函数级别的Fixture

    • setup_module/teardown_moudle:在当前文件中,在所有测试用例执行之前与之后执行。
    • setup_function/teardown_function:在每个测试函数之前与之后执行
    • setup/teardown:在每个测试函数之前鱼之后执行。这两个方法同样可以作用于类方法。

      ytest是支持使用测试类的,同样必须以“Test”开头,在引入测试类的情况下,Fixture的用法如下。创建test_fixtures_02.py文件

     1 # 功能函数
     2 def multiply(a, b):
     3     return a * b
     4 
     5 class TestMultiply:
     6     # ========Fixture===========
     7     @classmethod
     8     def setup_class(cls):
     9         print("setup_class======================>")
    10 
    11     @classmethod
    12     def teardown_class(cls):
    13         print("teardown_class========================>")
    14 
    15     def setup_method(self, method):
    16         print("setup_method------------------>")
    17 
    18     def teardown_method(self, method):
    19         print("teardown_method------------------->")
    20 
    21     def setup(self):
    22         print("setup.....................>")
    23 
    24     def cleardown(self):
    25         print("cleardown...................>")
    26 
    27 # ====================测试用例==================
    28     def test_numbers_5_6(self):
    29         print('test_numbers_5_6')
    30         assert multiply(5, 6) == 30
    31 
    32     def test_strings_b_2(self):
    33         print('test_strings_b_2')
    34         assert multiply('b', 2) == 'bb'

       这里主要用到类级别和方法级别的Fixture

    • setup_class/teardown_class:在当前测试类的开始与结束时执行
    • setup_method/teardown_method:在每个测试方法开始与结束时执行
    • setup/teardown:在每个测试方法开始与结束时执行,同样可以作用于测试函数。
  • 相关阅读:
    记一次由于缓存导致的bug
    3 Task中的一些枚举 创建时候的、continue时候的
    2 Task中的延续和7种阻塞
    1 Task的简单实用
    关于内存溢出遇到的两种情况
    6 Wcf使用Stream传输
    5 wcf双工
    4 WCF中的RPC和OneWay
    3 WCF一些基础铺垫
    什么是三元表达式?“三元”表示什么意思?
  • 原文地址:https://www.cnblogs.com/pegawayatstudying/p/12396720.html
Copyright © 2020-2023  润新知