202041412102 甘胜华
student.py:
class Student:
def __init__(self,name,score):
self.name = name
self.score = score
def __str__(self):
return self.name + "的成绩是" + self.score
student_manager.py:
from pytest_study.student import Student
class StudentManager:
def __init__(self):
self.students = list()
def add(self, student: Student):
self.students.append(student)
def remove(self, name: str):
index = -1
for student in self.students:
index += 1
if student.name == name:
return self.students.pop(index)
def show_students(self):
for student in self.students:
print(student)
def average_score(self):
score = 0
count = 0
for student in self.students:
score += student.score
count += 1
if count != 0:
return score / count
else:
return ZeroDivisionError
test_manager.py:
import allure
import pytest
from pytest_study.student import Student
from pytest_study.student_manager import StudentManager
@allure.feature("测试学生管理系统")
class TestManager:
def setup_class(self):
self.manager = StudentManager()
def setup_method(self):
print("测试开始")
def teardown_method(self):
print("测试结束")
@allure.story("测试添加学生功能")
@pytest.mark.add
@pytest.mark.parametrize("student, expect", [
(Student("小红", 50), 1),
(Student("小东", 60), 2),
(Student("小华", 70), 3),
(Student("小美", 80), 4),
(Student("小明", 90), 5)
])
def test_add(self, student, expect):
self.manager.add(student)
assert len(self.manager.students) == expect
@allure.story("测试删除学生功能")
@pytest.mark.dele
@pytest.mark.parametrize("name", ["小红", "小明"])
def test_remove(self, name):
student = self.manager.remove(name)
assert student.name == name
@allure.story("测试计算学生平均成绩功能")
@pytest.mark.avg
@pytest.mark.parametrize("score", [70])
def test_average_score(self, score):
avg_score = self.manager.average_score()
assert avg_score == score
报告: