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()为空时,执行的是该目录下所有的测试用例,加入参数可以指定运行