框架:
run.py
import pytest
import os
if __name__ == '__main__':
pytest.main()
os.system("allure generate temp -o reports --clean") #生成报告并且覆盖掉之前的报告
pytest.ini
[pytest]
addopts=-v -s "test_cases\deletefile_moudle\test_file.py::Test_file::test_create" --alluredir=temp --clean-alluredir
testpaths=./test_cases
conftest.py
import pytest
import requests
@pytest.fixture() #声明一个fixture 希望它在测试用例执行之前,执行,login不再是普通函数,而是pytest的fixture
def access_token():
url = "http://120.24.208.55:81/?user/index/loginSubmit&name=admin&password=box2046"
payload = {}
headers = {
'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
}
response = requests.post(url=url, headers=headers, data=payload).json()
return response["info"]
test_cases / deletefile_moudle / test_file.py
import requests
class Test_file:
def test_create(self,access_token):
url = "http://120.24.208.55:81/?explorer/index/mkdir"
payload={"path":"{source:1}/222"}
headers = {
"access_token": f"{access_token}",
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
}
response = requests.post(url=url, headers=headers, data=payload)
global inf
inf=response.json()["info"]
#=================断言================================
assert response.json()["code"]==True,"创建文件夹用例失败"
def test_upload(self,access_token):
url = "http://120.24.208.55:81/?explorer/upload/fileUpload"
payload = {'path': f'{inf}'}
files = [
('name', ('1.docx', open('/C:/Users/administraors/Desktop/1.docx', 'rb'),
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'))
]
headers = {
'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1',
'accessToken': f'{access_token}'
}
response = requests.post(url=url, headers=headers, data=payload, files=files)
# =================断言================================
assert response.json()["code"] ==True, "上传文件用例失败"
def test_delete(self,access_token):
pass
def test_exit(self,access_token):
pass
test_cases / login_moudle / test_login.py
import requests
def test_login():
url = "http://120.24.208.55:81/?user/index/loginSubmit&name=admin&password=box2046"
payload={}
headers = {
'Cookie': 'CSRF_TOKEN=AueSvH9zycZpTDmu; KOD_SESSION_ID=112fe71aa47de18e72856efdf047ba77; kodUserID=1'
}
response = requests.post(url=url, headers=headers, data=payload)
#===============断言=====================
assert response.json()["code"]==True,"用例测试不通过"