小型购物网站建设,世界交互设计最好的前10大学,wordpress 防爆破,关键词优化seo外包首先确保安装了Python环境#xff0c;首先#xff0c;你需要确保已安装 Python 和 Pip。如果还没有安装#xff0c;可以从 Python 官方网站下载并安装最新版本的 Python。安装过程中请确保选中“Add Python to PATH”选项。
安装pytest:打开命令提示符#xff08;Command …首先确保安装了Python环境首先你需要确保已安装 Python 和 Pip。如果还没有安装可以从 Python 官方网站下载并安装最新版本的 Python。安装过程中请确保选中“Add Python to PATH”选项。
安装pytest:打开命令提示符Command Prompt或 PowerShell运行以下命令来安装 Pytest 和 Requests 库
pip install pytest requests之后查看是否安装成功
pip show pytest 之后搭建一个后端项目以springboot项目为例
编写一个简单的模拟登录的接口之后启动项目
RestController
RequestMapping(/aa)
public class TestController {Autowiredprivate UserPoService userPoService;GetMapping(/testLogin)LogExecutionTimepublic String test(String name,String password){if(userPoService.testLogin(name,password)){return success!!;}return error;}以上代码的逻辑就是 根据用户传过来的name字段查询数据库之后拿到用户信息之后和password字段作对比如果密码正确就返回success 失败就是 error
之后编写python脚本
1. 用错误的密码尝试
import requests
import pytestpytest.fixture
def api_url():return http://localhost:8080/aa # 假设你的接口地址是这个def test_login(api_url):response requests.get(f{api_url}/testLogin?name平安password111)# 打印响应详细信息print(Status Code:, response.status_code)print(Headers:, response.headers)print(Response Text:, response.text)# 断言部分assert response.status_code 200assert response.headers[Content-Type] application/jsonassert success!! in response.text查看运行结果 2.关掉后端服务模拟服务器宕机 3. 使用正确的账号密码
import requests
import pytestpytest.fixture
def api_url():return http://localhost:8080/aa # 假设你的接口地址是这个def test_login(api_url):response requests.get(f{api_url}/testLogin?name平安password7,23)# 打印响应详细信息print(Status Code:, response.status_code)print(Headers:, response.headers)print(Response Text:, response.text)# 断言部分assert response.status_code 200assert response.headers[Content-Type] text/plain;charsetUTF-8assert success!! in response.text