pytest基础知识2

pycharm常用的快捷键:
Ctrl + Shift + F 或者连续2次敲击shift 全局查找{可以在整个项目中查找某个字符串什么的,如查找某个函数名字符串看之前是怎么使用这个函数的}
Ctrl + B 针对某个内置方法可以链接查看它的源代码

pytest参数化

class TestCalc:

    def setup(self) -> None:
        self.calc=Calc()

    @pytest.mark.parametrize("a, b, c", [
        (1, 1, 2),
        (1, 0, 1),
        (1, -1, 0),
        (1, 1000000, 1000001)
    ])
    def test_add(self, a, b , c):
        print(a,b,c)
        assert self.calc.add(a, b)==c

json格式文件 calc.json

[
{“a”: 2, “b”: 1, “c”: 2},
{“a”: 3, “b”: 1, “c”: 4},
{“a”: 24, “b”: 1, “c”: 25}
]

读取json文件的方法:

def test_json():
	f=open("calc.json")
	r=load(f)
	print ( r )

yaml格式文件calc.yaml

-   a: 1
    b: 2
    c: 3
-   a: 1
    b: -1
    c= 0
-   a: 100000
    b: 1
    c: 100001

@pytest.mark.paramtrize(“a,b,c”,json.load(“calc.json”))

pytest的fixture用法

源代码

import requests

def test_1():
    json=requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
    assert len(json["topics"]) == 2

def test_2():
    json=requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()
    assert json["topics"][0]["deleted"] == False

加入fixture之后的代码

import pytest
import requests

@pytest.fixture()
def topics():
    return requests.get("https://testerhome.com/api/v3/topics.json?limit=2").json()

def test_1(topics):
    assert len(topics["topics"]) == 2

def test_2(topics):
    assert topics["topics"][0]["deleted"] == False

运行allure测试报告

$ pytest --alluredir=/tmp/my_allure_results
allure serve /tmp…

使用pytest运行可以在,代码中加入

if __name__ == '__main__':
    pytest.main(["-q","test_pytest_param.py"])

main()为空时,执行的是该目录下所有的测试用例,加入参数可以指定运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值