美文网首页
(六)pytest-参数化

(六)pytest-参数化

作者: Sandra_liu | 来源:发表于2021-07-28 20:46 被阅读0次

    1、参数化
    json文件

    [
      [
        2,
        1,
        3
      ],
      [
        2,
        5,
        3
      ]
    ]
    

    yaml文件

    - - 2
      - 1
      - 3
    - - 2
      - 5
      - 3
    
    import json
    import logging
    import os
    
    import pytest
    import yaml
    
    
    class Test_B:
    
        # argvalues为string
        @pytest.mark.smoke  #标记用例为冒烟测试用例
        @pytest.mark.parametrize("a,b,c", [(1, 1, 2), (1000, 1000, 2000), (-1, -1, -1)])
        def test_add_004(self, a, b, c):
            logging.info("4")
            assert self.calc.add(self, a, b) == c
    
        # argvalues为tumple
        @pytest.mark.skip   #标记不需要执行的测试用例
        @pytest.mark.parametrize(("a","b","c"), [(1, 1, 2), (1000, 1000, 2000), (-1, -1, -1)])
        def test_add_004_1(self, a, b, c):
            logging.info("4_1")
            assert self.calc.add(self, a, b) == c
    
        # argvalues为list
        @pytest.mark.skipif  #标记不需要执行的测试用例
        @pytest.mark.parametrize(["a", "b", "c"], [(1, 1, 2), (1000, 1000, 2000), (-1, -1, -1)])
        def test_add_004_2(self, a, b, c):
            logging.info("4_2")
            assert self.calc.add(self, a, b) == c
    
        #在json中获取数据
        @pytest.mark.xfail  #标记预期会失败的用例
        @pytest.mark.parametrize("a, b, c", json.load(open("./calc.json")))
        def test_add_005(self, a, b, c):
            logging.info("5")
            # print(os.getcwd() + "/calc.json")
            assert a+b == c
    
        #在yaml中获取数据
        @pytest.mark.parametrize("a, b, c", yaml.load(open("./calc.yaml"),Loader=yaml.FullLoader))
        def test_add_006(self,a, b, c):
            logging.info("6")
            # print(os.getcwd()+"/calc.yaml")
            assert a+b == c
    
    if __name__ == '__main__':
        pytest.main(["-q", "--log-level=INFO", "-s", "test_mark.py"])
    

    2、可能出现的问题:
    (1)YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated...
    解决方案:data = yaml.load(file_data, Loader=yaml.FullLoader)

    (2)不打印logging日志
    解决方案:--log-level=INFO

    (3)json/yaml文件:
    通过命令行生成allure测试报告,需要加上测试用例所在的路径

    (4)yaml文件需要安装pyaml

    相关文章

      网友评论

          本文标题:(六)pytest-参数化

          本文链接:https://www.haomeiwen.com/subject/fvhtvltx.html