Skip to content
0
  • Categories
  • 讨论
  • Recent
  • Tags
  • Popular
  • Top
  • Users
  • Categories
  • 讨论
  • Recent
  • Tags
  • Popular
  • Top
  • Users
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Zephyr)
  • No Skin
Collapse
JIKE社区
  1. 首页
  2. 讨论区
  3. 技术专区
  4. 本社区终于可以向 @ChatGPT 提问了!

本社区终于可以向 @ChatGPT 提问了!

Scheduled Pinned Locked Moved 技术专区
11.5k Posts 326 Posters 49.5m Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 5 Offline
    5 Offline
    502659689
    wrote on last edited by
    #1901

    @ChatGPT word如何群发邮件?

    ChatGPTC 1 Reply Last reply
    0
    • 5 502659689

      @ChatGPT word如何群发邮件?

      ChatGPTC Offline
      ChatGPTC Offline
      ChatGPT
      wrote on last edited by
      #1902

      502659689说:

      word如何群发邮件?

      Step 1: Compose a message.
      Step 2: Select the recipients.
      Step 3: Click the “Send” button.

      帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

      1 Reply Last reply
      0
      • ? This user is from outside of this forum
        ? This user is from outside of this forum
        Guest
        wrote on last edited by
        #1903

        @ChatGPT FastAPI单元测试讲解实例,以学生管理为例

        ChatGPTC 1 Reply Last reply
        0
        • ? Guest

          @ChatGPT FastAPI单元测试讲解实例,以学生管理为例

          ChatGPTC Offline
          ChatGPTC Offline
          ChatGPT
          wrote on last edited by
          #1904

          guest说:

          FastAPI单元测试讲解实例,以学生管理为例

          目录

          • 1、准备工作
          • 2、编写测试代码
            • 2.1 测试获取学生列表
            • 2.2 测试创建学生
            • 2.3 测试更新学生信息
            • 2.4 测试删除学生
          • 3、编写测试执行程序

          1、准备工作

          在开始编写测试代码之前,我们需要准备一些必要的工作:

          • 安装pytest

            要进行FastAPI的单元测试,我们需要安装pytest,它是一款功能强大的Python测试框架,支持测试用例的编写、执行以及结果的收集,所以我们需要先安装它:

            $ pip install pytest
            
          • 安装uvicorn

            uvicorn是一款异步非阻塞的Python Web服务器,它支持ASGI协议,所以我们可以使用它来启动我们的FastAPI应用,并用于进行测试:

            $ pip install uvicorn
            
          • 编写FastAPI应用

            为了方便讲解,我们编写一个简单的学生管理的FastAPI应用:

            # main.py
            from typing import List
            from fastapi import FastAPI, Depends, HTTPException
            from pydantic import BaseModel
            
            
            class Student(BaseModel):
                name: str
                age: int
                gender: str
            
            
            students = [
                {
                    "name": "Bob",
                    "age": 20,
                    "gender": "male"
                },
                {
                    "name": "Alice",
                    "age": 18,
                    "gender": "female"
                }
            ]
            
            
            app = FastAPI()
            
            
            @app.get("/students/")
            async def get_students():
                return students
            
            
            @app.post("/students/")
            async def create_student(student: Student):
                students.append(student.dict())
                return student
            
            
            @app.put("/students/{name}")
            async def update_student(name: str, student: Student):
                for index, item in enumerate(students):
                    if item["name"] == name:
                        students[index] = student.dict()
                        break
                else:
                    raise HTTPException(status_code=404, detail="Student not found")
                return student
            
            
            @app.delete("/students/{name}")
            async def delete_student(name: str):
                for index, item in enumerate(students):
                    if item["name"] == name:
                        del students[index]
                        break
                else:
                    raise HTTPException(status_code=404, detail="Student not found")
                return {"msg": "delete success"}
            

          2、编写测试代码

          2.1 测试获取学生列表

          首先,我们来测试获取学生列表的接口,编写测试用例:

          # test_main.py
          import json
          from typing import List
          
          import pytest
          import requests
          
          from main import app
          
          
          @pytest.fixture(scope="module")
          def test_client():
              test_client = app.test_client()
              return test_client
          
          
          def test_get_students(test_client):
              response = test_client.get("/students/")
              data = json.loads(response.data)
              assert len(data) == 2
              students = [
                  {
                      "name": "Bob",
                      "age": 20,
                      "gender": "male"
                  },
                  {
                      "name": "Alice",
                      "age": 18,
                      "gender": "female"
                  }
              ]
              assert data == students
          

          测试用例:

          • 先通过pytest.fixture定义一个fixture,用于创建一个FastAPI的客户端,用于发送测试请求;
          • 然后编写测试函数,函数参数为上面定义的fixture,这样pytest就会自动执行fixture,将它的返回值传入测试函数;
          • 通过客户端发送GET请求,验证返回结果是否正确。

          2.2 测试创建学生

          接下来,我们来测试创建学生的接口,编写测试用例:

          # test_main.py
          import json
          
          import pytest
          
          from main import app
          
          
          @pytest.fixture(scope="module")
          def test_client():
              test_client = app.test_client()
              return test_client
          
          
          def test_create_student(test_client):
              # 构造数据
              data = {
                  "name": "Tom",
                  "age": 20,
                  "gender": "male"
              }
              # 发送请求
              response = test_client.post("/students/", json=data)
              # 验证结果
              assert response.status_code == 200
              data = json.loads(response.data)
              assert data == data
          

          测试用例:

          • 通过pytest.fixture定义一个fixture,用于创建一个FastAPI的客户端;
          • 编写测试函数,函数参数为上面定义的fixture;
          • 构造测试数据;
          • 通过客户端发送POST请求;
          • 验证返回结果是否正确。

          2.3 测试更新学生信息

          接下来,我们来测试更新学生信息的接口,编写测试用例:

          # test_main.py
          import json
          
          import pytest
          
          from main import app
          
          
          @pytest.fixture(scope="module")
          def test_client():
              test_client = app.test_client()
              return test_client
          
          
          def test_update_student(test_client):
              # 构造数据
              data = {
                  "name": "Bob",
                  "age": 21,
                  "gender": "male"
              }
              # 发送请求
              response = test_client.put("/students/Bob", json=data)
              # 验证结果
              assert response.status_code == 200
              data = json.loads(response.data)
              assert data == data
          

          测试用例:

          • 通过pytest.fixture定义一个fixture,用于创建一个FastAPI的客户端;
          • 编写测试函数,函数参数为上面定义的fixture;
          • 构造测试数据;
          • 通过客户端发送PUT请求;
          • 验证返回结果是否正确。

          2.4 测试删除学生

          最后,我们来测试删除学生的接口,编写测试用例:

          # test_main.py
          import json
          
          import pytest
          
          from main import app
          
          
          @pytest.fixture(scope="module")
          def test_client():
              test_client = app.test_client()
              return test_client
          
          
          def test_delete_student(test_client):
              # 发送请求
              response = test_client.delete("/students/Bob")
              # 验证结果
              assert response.status_code == 200
              data = json.loads(response.data)
              assert data == {"msg": "delete success"}
          

          测试用例:

          • 通过pytest.fixture定义一个fixture,用于创建一个FastAPI的客户端;
          • 编写测试函数,函数参数为上面定义的fixture;
          • 通过客户端发送DELETE请求;
          • 验证返回结果是否正确。

          3、编写测试执行程序

          帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #1905

            @ChatGPT 举例讲解下FastAPI的单元测试,以学生信息管理为例,举例讲解下原理和使用方法

            ChatGPTC 1 Reply Last reply
            0
            • ? Guest

              @ChatGPT 举例讲解下FastAPI的单元测试,以学生信息管理为例,举例讲解下原理和使用方法

              ChatGPTC Offline
              ChatGPTC Offline
              ChatGPT
              wrote on last edited by
              #1906

              guest说:

              举例讲解下FastAPI的单元测试,以学生信息管理为例,举例讲解下原理和使用方法

              1. 单元测试的原理:

              单元测试是指分离的测试,它的目的是检查每个独立的单元是否正确工作,单元测试可以帮助检测单元之间的相互依赖,从而确保程序的稳定性和正确性。

              1. 使用FastAPI进行单元测试:

              (1)安装pytest,并在项目根目录创建 tests/ 目录;

              (2)在 tests/ 目录下创建一个 test_students.py 文件,用来编写学生信息管理的测试用例;

              (3)使用 pytest 在 tests/ 目录下执行 test_students.py 文件,检查测试用例是否正确;

              (4)如果测试用例通过,则代表学生信息管理的功能是正常工作的。

              帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

              1 Reply Last reply
              0
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                Guest
                wrote on last edited by
                #1907

                我想知道俄乌战争近况

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #1908

                  能问医疗方面的知识吗

                  ? 1 Reply Last reply
                  0
                  • ? Guest

                    能问医疗方面的知识吗

                    ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on last edited by
                    #1909

                    游客 说:

                    能问医疗方面的知识吗

                    1 Reply Last reply
                    0
                    • ? This user is from outside of this forum
                      ? This user is from outside of this forum
                      Guest
                      wrote on last edited by
                      #1910

                      @ChatGPT 你懂医疗方面的知识吗?

                      ChatGPTC ? 2 Replies Last reply
                      0
                      • ? Guest

                        @ChatGPT 你懂医疗方面的知识吗?

                        ChatGPTC Offline
                        ChatGPTC Offline
                        ChatGPT
                        wrote on last edited by
                        #1911

                        guest说:

                        你懂医疗方面的知识吗?

                        不太懂,但可以学习。

                        帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

                        1 Reply Last reply
                        0
                        • ? Guest

                          @ChatGPT 你懂医疗方面的知识吗?

                          ? This user is from outside of this forum
                          ? This user is from outside of this forum
                          Guest
                          wrote on last edited by
                          #1912

                          游客 说:

                          @ChatGPT 你懂医疗方面的知识吗?

                          1 Reply Last reply
                          0
                          • ? This user is from outside of this forum
                            ? This user is from outside of this forum
                            Guest
                            wrote on last edited by
                            #1913

                            粗体字

                            1 Reply Last reply
                            0
                            • ? This user is from outside of this forum
                              ? This user is from outside of this forum
                              Guest
                              wrote on last edited by
                              #1914

                              @ChatGPT 知道我在网络上的信息吗?

                              ChatGPTC 1 Reply Last reply
                              0
                              • ? Guest

                                @ChatGPT 知道我在网络上的信息吗?

                                ChatGPTC Offline
                                ChatGPTC Offline
                                ChatGPT
                                wrote on last edited by
                                #1915

                                guest说:

                                知道我在网络上的信息吗?

                                这取决于你在网络上所发布的信息以及你提供给网络的信息是否是公开的。如果你在网络上发布的信息是隐私的,那么只有你自己才知道。

                                帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

                                1 Reply Last reply
                                0
                                • 戨 Offline
                                  戨 Offline
                                  戨cf
                                  wrote on last edited by
                                  #1916

                                  @RuYoChatBot存在四维度吗

                                  1 Reply Last reply
                                  0
                                  • ? This user is from outside of this forum
                                    ? This user is from outside of this forum
                                    Guest
                                    wrote on last edited by
                                    #1917

                                    麦当劳好吃还是肯德基

                                    1 Reply Last reply
                                    0
                                    • ? This user is from outside of this forum
                                      ? This user is from outside of this forum
                                      Guest
                                      wrote on last edited by
                                      #1918

                                      世界杯阿根廷和法国那个是冠军?

                                      1 Reply Last reply
                                      0
                                      • ? This user is from outside of this forum
                                        ? This user is from outside of this forum
                                        Guest
                                        wrote on last edited by
                                        #1919

                                        @ChatGPT 新冠病毒是不是人为制造出来的

                                        ChatGPTC 1 Reply Last reply
                                        0
                                        • ? Guest

                                          @ChatGPT 新冠病毒是不是人为制造出来的

                                          ChatGPTC Offline
                                          ChatGPTC Offline
                                          ChatGPT
                                          wrote on last edited by
                                          #1920

                                          guest说:

                                          新冠病毒是不是人为制造出来的

                                          ?

                                          不是,新冠病毒是一种自然发生的病毒,它是由动物传播给人类的。

                                          帖子内容中 @ChatGPT(注意前后空格,发帖后名称变色才行)。必须@才会回答

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • 1
                                          • 2
                                          • 94
                                          • 95
                                          • 96
                                          • 97
                                          • 98
                                          • 576
                                          • 577
                                          Popular Topics - 热门主题
                                          • 支付宝余额免费提现4种方式教程
                                            undefined
                                            1
                                            1
                                            580

                                          • 免费油管音乐客户端:Metrolist
                                            undefined
                                            1
                                            1
                                            129

                                          • 速度!9.9开美团年卡(12个月每月10张5元无门槛券)
                                            A
                                            abcpython
                                            0
                                            3
                                            343

                                          • 美団APP-首页~美团外卖点进去
                                            H
                                            ‍
                                            0
                                            2
                                            152

                                          • 盘古之殇:华为诺亚盘古大模型研发历程的心酸与黑暗
                                            孤
                                            孤傲的王
                                            0
                                            2
                                            67

                                          • 多人有声剧《稳住别浪》主播:佳喻声社 1248集完
                                            undefined
                                            0
                                            1
                                            2

                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          百度网盘
                                          1,569 topics
                                          连续剧
                                          773 topics
                                          国产剧
                                          603 topics
                                          美剧
                                          359 topics
                                          中国移动
                                          307 topics
                                          动画片
                                          264 topics
                                          动作片
                                          223 topics
                                          中国电信
                                          222 topics

                                          129

                                          Online

                                          40.9k

                                          Users

                                          40.7k

                                          Topics

                                          111.2k

                                          Posts
                                          • First post
                                            Last post