帮我写的单元测试框架Unittest

2023-04-24 14:19:15 浏览数 (1)

智造喵GPT地址:https://chat.plexpt.com/i/511440

以下是一个简单的Python unittest requests单元测试框架的示例:

import unittest

import requests

class TestAPI(unittest.TestCase):

def setUp(self):

self.base_url = "http://api.example.com"

self.headers = {"Content-Type": "application/json"}

def test_get_user(self):

url = self.base_url "/user"

r = requests.get(url, headers=self.headers)

self.assertEqual(r.status_code, 200)

self.assertEqual(r.json()["name"], "Alice")

def test_create_user(self):

url = self.base_url "/user"

data = {"name": "Bob", "age": 30}

r = requests.post(url, headers=self.headers, json=data)

self.assertEqual(r.status_code, 201)

self.assertEqual(r.json()["name"], "Bob")

self.assertEqual(r.json()["age"], 30)

def test_update_user(self):

url = self.base_url "/user/1"

data = {"age": 31}

r = requests.put(url, headers=self.headers, json=data)

self.assertEqual(r.status_code, 200)

self.assertEqual(r.json()["age"], 31)

def test_delete_user(self):

url = self.base_url "/user/1"

r = requests.delete(url, headers=self.headers)

self.assertEqual(r.status_code, 204)

if __name__ == '__main__':

unittest.main()

0 人点赞