shell脚本写的班级管理系统测试用例

简介: shell脚本写的班级管理系统测试用例

假设我们要测试一个简单的班级管理系统,其中包括学生的姓名、年龄、成绩三个属性。下面是一个简单的学生信息增删改查的操作示例:
首先,我们需要先初始化班级和学生的信息:

# 初始化班级信息
init_classroom() {
     
    curl -X POST -d '{"name":"class1"}' http://localhost:8080/classroom 
}

# 初始化学生信息
init_student() {
   
    for i in {
   1..3}; do
        echo "{\"name\":\"student${i}\",\"age\":$((i+10)),\"score\":$((i*50))}" | \
        curl -X POST -H "Content-Type: application/json" -d @- http://localhost:8080/student
    done
}

接下来,我们可以写几个简单的测试用例:

  • 创建一个新班级:
    test_add_classroom() {
         
    local classroom_id=$(curl -X POST -d "{\"name\":\"class2\"}" http://localhost:8080/classroom | jq -r '.id')
    assertEquals "class2" "$(curl -X GET "http://localhost:8080/classroom/${classroom_id}/info" | jq -r '.name')"
    }
    
  • 删除一个班级:
    test_remove_classroom() {
         
    local classroom_id=$(curl -X POST -d "{\"name\":\"class3\"}" http://localhost:8080/classroom | jq -r '.id')
    assertEquals "200" "$(curl -X DELETE "http://localhost:8080/classroom/${classroom_id}" | jq -r '.code')"
    }
    
  • 更新一个班级:

    test_update_classroom() {
         
    local classroom_id=$(curl -X POST -d "{\"name\":\"class4\"}" http://localhost:8080/classroom | jq -r '.id')
    assertEquals "200" "$(curl -X PUT -d "{
         \"name\":\"new_name\"}" "http://localhost:8080/classroom/${classroom_id}" | jq -r '.code')"
    
    assertEquals "new_name" "$(curl -X GET "http://localhost:8080/classroom/${classroom_id}/info" | jq -r '.name')"
    }
    
  • 查询学生信息:
    test_query_students() {
         
    local student_ids=$(curl -X GET "http://localhost:8080/students" | jq -r '.[] | .id')
    for student_id in $student_ids; do
        assertEquals "student$i" "$(curl -X GET "http://localhost:8080/student/${student_id}/info" | jq -r '.name')"
    done
    }
    
  • 添加一个学生:
    test_add_student() {
         
    local student_id=$(curl -X POST -d "{\"name\":\"student4\",\"age\":40,\"score\":200}" http://localhost:8080/student | jq -r '.id')
    assertEquals "200" "$(curl -X GET "http://localhost:8080/student/${student_id}/info" | jq -r '.code')"
    assertEquals "student4" "$(curl -X GET "http://localhost:8080/student/${student_id}/info" | jq -r '.name')"
    }
    
  • 修改一个学生:

    test_update_student() {
         
    local student_id=$(curl -X POST -d "{\"name\":\"student5\",\"age\":50,\"score\":250}" http://localhost:8080/student | jq -r '.id')
    assertEquals "200" "$(curl -X PUT -d "{
         \"name\":\"new_name\"}" "http://localhost:8080/student/${student_id}" | jq -r '.code')"
    
    assertEquals "new_name" "$(curl -X GET "http://localhost:8080/student/${student_id}/info" | jq -r '.name')"
    }
    
  • 删除一个学生:
    test_remove_student() {
         
    local student_id=$(curl -X POST -d "{\"name\":\"student6\",\"age\":60,\"score\":300}" http://localhost:8080/student | jq -r '.id')
    assertEquals "200" "$(curl -X DELETE "http://localhost:8080/student/${student_id}" | jq -r '.code')"
    }
    

最后,我们可以定义一个函数来运行所有的测试用例:

run_test() {
   
    init_classroom
    init_student
    test_add_classroom
    test_remove_classroom
    test_update_classroom
    test_query_students
    test_add_student
    test_update_
相关文章
|
26天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
6天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
33 3
|
1天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(61)
【4月更文挑战第26天】shell脚本解析及训练(61)
12 3
|
1天前
|
弹性计算 运维 Shell
每天解析一个shell脚本(58)
【4月更文挑战第26天】shell脚本解析及训练(58)
67 0
|
1天前
|
弹性计算 Shell 数据安全/隐私保护
每天解析一个shell脚本(56)
【4月更文挑战第26天】shell脚本解析及训练(56)
13 0
|
3天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
3天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)
|
3天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
3天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
9天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
37 0