zoukankan      html  css  js  c++  java
  • pytest实例

    一、题目:

          实现一个测试用例集合,包含3个类TestA TestB TestC,每个类包含3个方法,总共9个方法test_1到test_9,完成特定动作只要print打印即可。执行顺序如下:

          1、测试module开始执行

          2、TestA

          3、TestA teardown清理环境

          4、TestB每个方法都要有初始化,但是没有teardown

          5、TestB的3个方法包含3种参数化用例

          6、TestC的执行顺序是test8 test9 test7

         7、整个module完成执行清理环境

    # -*- coding: utf-8 -*- 
    # @Time : 2020/9/17 10:28 
    # @Author : hl
    # @File : test_word01.py
    
    import pytest
    import json
    import yaml
    
    def setup_module():
        print("
    测试module开始执行")
    
    def teardown_module():
        print("
    整个module完成执行清理环境")
    
    #第一个类TestA
    class  TestA(object):
        @classmethod
        def teardown_class(cls):
            print("
    TestA teardown清理环境")
    
        @pytest.mark.run(order=1)
        def test_1(self):
            print("开始执行TestA中的方法了:")
            print("
    test_1,测试用例1")
    
        @pytest.mark.run(order=2)
        def test_2(self):
            print("
    test_2,测试用例2")
    
        @pytest.mark.run(order=3)
        def test_3(self):
            print("
    test_3,测试用例3")
    
    #第二个类TestB
    class  TestB(object):
        def setup_method(self):
            print("
    setup_method1,每个测试方法都执行一次")
    
        @pytest.mark.run(order=4)
        @pytest.mark.parametrize("a,b,c",[
            (1,1,2),
            (1,0,1),
            (1,-1,0)
        ])
        def test_4(self,a,b,c):
            print("test_4,测试用例4")
            print(a,b,c)
            assert a+b == c
    
        @pytest.mark.run(order=5)
        @pytest.mark.parametrize("a,b,c",json.load(open(r"C:Users洛北11PycharmProjects
    sxqpytest_demo5.json")))
        def test_5(self,a,b,c):
            print("test_5,测试用例5")
            print(a,b,c)
            assert a+b == c
    
        @pytest.mark.run(order=6)
        @pytest.mark.parametrize("a,b,c", yaml.full_load(open(r"C:Users洛北11PycharmProjects
    sxqpytest_demo6.yaml")))
        def test_6(self,a,b,c):
            print("test_6,测试用例6")
            print(a,b,c)
            assert a+b == c
    
    #第三个类TestC
    class  TestC(object):
    
        @pytest.mark.run(order=9)
        def test_7(self):
            print("test_7,测试用例7")
            assert 1==1
    
        @pytest.mark.run(order=7)
        def test_8(self):
            print("test_8,测试用例8")
            assert 1 == 1
    
        @pytest.mark.run(order=8)
        def test_9(self):
            print("test_9,测试用例9")
            assert 1 == 1
    
    if __name__ == "__main__":
        pytest.main(["-s","test_word01.py"])    #在运行结果中打印 print 输出的代码

    5.json:

    [
      [1,2,3],
      [2,3,5],
      [4,5,9]
    ]

    6.yaml:

    #6.yaml
    #[['1,1,2'], ['2,2,4'], ['1,2,3']]
    
    -
      - 1
      - 1
      - 2
    -
      - 2
      - 2
      - 4
    -
      - 1
      - 2
      - 3
         
  • 相关阅读:
    linux下运行、停止jar包
    .net开发CAD2008无法调试的解决方法
    【转】C#获取当前程序运行路径的方法集合
    如何在arcmap中调试addin或者插件
    【转】WriteMessage的信息在AutoCAD中命令行中实时显示
    ArcGIS10的附件功能
    CAD调试时抛出“正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码”异常的解决方法
    [转]Tesseract 3.02中文字库训练
    Truncated incorrect DOUBLE value
    tomcat无故停止
  • 原文地址:https://www.cnblogs.com/hl-2030/p/13689969.html
Copyright © 2011-2022 走看看