python单元测试框架

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

报告:

学号202041412115 姓名 欧明朗
image
image
image

image




202041412119 王寅

[student.py]

class Student:
    def __init__(self, name, score):
        self.name = name
        self.score = score

    def __str__(self):
        return f"姓名:{self.name},分数:{self.score}"

student_manager.py

class StudentManager:
    def __init__(self):
        self.students = []

    def add(self, student):
        """
        添加学员
        :param student: 学生对象
        """
        self.students.append(student)

    def remove(self, name):
        """
        删除学员
        :param name: 学生姓名
        """
        for student in self.students:
            if student.name == name:
                self.students.remove(student)
                break

    def show_students(self):
        """
        显示所有学员信息
        """
        for student in self.students:
            print(student)

    def calculate_average_score(self):
        """
        计算学员平均分数
        :return: 平均分数
        """
        total_score = 0
        for student in self.students:
            total_score += student.score
        average_score = total_score / len(self.students)
        return average_score

202041404214 刘均豪
452fa8e28450840b49dd043b3b82c0e

class StudentManager:

    def __init__(self):
        self.students=[]

    def add(self, student):
        """
        添加学员
        :param student:
        :return:
        """
        self.students.append(student)

    def remove(self, name: str):
        """
        删除学员
        :param name:
        :return:
        """
        for student in self.students:
            if student.name==name:
                self.students.remove(student)
                break

    def show_students(self):
        """
        显式所有的学员信息
        :return:
        """
        for student in self.students:
            print(student)

    def average_score(self):
        """
        计算学员的平均成绩
        :return:
        """
        total_score = 0
        num_students = len(self.students)

        for student in self.students:
            total_score += student.score

        if num_students > 0:
            average = total_score / num_students
            return average
        else:
            return 0.0
from student import Student
from student_manager import StudentManager
import pytest
class Test_01:
    def setup(self):
        self.manager = StudentManager()
        print("测试开始")
    def test_add(self):
          student=Student("alice",80)
          self.manager .add(student)
          assert student in self.manager.students
    def test_show(self):
        student = Student("alice",80)
        self.manager .add(student)
    def test_nemove(self):
        student = Student("alice",80)
        self.manager.add(student)
        self.manager .remove(student.name)
    def test_average(self):
        student = Student("alice",80)
        student2=Student("bb",900)
        self.manager.add(student)
        self.manager .add(student2)
        avg=self.manager.average_score()
        assert avg==490
if __name__ =='__main__':
    test=Test_01()

202041404211 李范佑

student

class Student:
    def __init__(self,name,score):
        self.name = name
        self.score = score

    def __str__(self):
        return f"Student(name={self.name}, score={self.score})"

student_manager

from student import Student


class StudentManager:

    def __init__(self):
        self.students = []

    def add(self, student):
        """
        添加学员
        :param student:
        :return:
        """
        self.students.append(student)

    def remove(self, name: str):
        """
        删除学员
        :param name:
        :return:
        """
        for student in self.students:
            if student.name == name:
                self.students.remove(student)
                break

    def show_students(self):
        """
        显式所有的学员信息
        :return:
        """
        for student in self.students:
            print(student)

    def average_score(self):
        """
        计算学员的平均成绩
        :return:
        """
        total_score = 0
        for student in self.students:
            total_score += student.score

        if len(self.students) > 0:
            average_score = total_score / len(self.students)
            return average_score
        else:
            return 0

test_student_manager

import pytest
from student import Student
from student_manager import StudentManager


@pytest.fixture
def student_manager():
    return StudentManager()


def test_add_student(student_manager):
    student = Student("Alice", 90)
    student_manager.add(student)
    assert len(student_manager.students) == 1

def test_show(student_manager):
    student_manager.show_students()

def test_remove_student(student_manager):
    student = Student("Bob", 85)
    student_manager.add(student)
    student_manager.remove("Bob")
    assert len(student_manager.students) == 0

def test_average_score(student_manager):
    student1 = Student("Charlie", 70)
    student2 = Student("David", 80)
    student_manager.add(student1)
    student_manager.add(student2)
    assert student_manager.average_score() == 75

202041404109 胡智


4
5
6

姓名:李升升
学号:202041404115
image



class Student:

def __init__(self, name, score):
    self.name = name
    self.score = score

def __str__(self):
    return f"Student(name={self.name}, score={self.score})"

class StudentManager:

def __init__(self):
    self.students = []

def add(self, student):
    """
    添加学员
    :param student:
    :return:
    """
    self.students.append(student)

def remove(self, name):
    """
    删除学员
    :param name:
    :return:
    """
    for student in self.students:
        if student.name == name:
            self.students.remove(student)
            break

def show_students(self):
    """
    显式所有的学员信息
    :return:
    """
    for student in self.students:
        print(student)

def average_score(self):
    """
    计算学员的平均成绩
    :return:
    """
    total_score = 0
    num_students = len(self.students)

    for student in self.students:
        total_score += student.score

    if num_students > 0:
        average = total_score / num_students
        return average
    else:
        return 0.0

import pytest
from student import Student
from student_manager import StudentManager

class TestStudentManager:

def setup_class(self):
    self.student_manager = StudentManager()

def setup(self):
    print("开始计算")

def teardown(self):
    print("结束计算")

def teardown_class(self):
    print("结束测试")

@pytest.mark.add
@pytest.mark.run(order=1)
@pytest.mark.parametrize("student, expect", [
    (Student("张三", 60), 1),
    (Student("李四", 70), 2),
    (Student("王五", 80), 3),
    (Student("赵六", 90), 4),
    (Student("孙七", 100), 5)
])
def test_add_student(self, student, expect):
    self.student_manager.add(student)
    assert len(self.student_manager.students) == expect

@pytest.mark.add
@pytest.mark.run(order=2)
@pytest.mark.parametrize("name, expect", [("周八", 5), ("孙七", 4), ("赵六", 3)])
def test_remove_student(self, name, expect):
    self.student_manager.remove(name)
    assert len(self.student_manager.students) == expect

@pytest.mark.add
@pytest.mark.run(order=3)
@pytest.mark.parametrize("expect", ["Student(name=张三, score=60)\nStudent(name=李四, score=70)\nStudent(name=王五, score=80)\n"])
def test_show_students(self, capsys, expect):
    self.student_manager.show_students()
    captured = capsys.readouterr().out
    assert captured == expect

@pytest.mark.add
@pytest.mark.run(order=3)
@pytest.mark.parametrize("expect", [70])
def test_average_score(self, expect):
    average = self.student_manager.average_score()
    assert average == expect

彭子晴 202041404120
student.py:
class Student:

def __init__(self, name, score):
    self.name = name
    self.score = score

def __str__(self):
    return f"{self.name},{self.score}"

student_manager.py:

from student import Student

class StudentManager:

def __init__(self):
    self.students = []

def add(self, student: Student):
    """
    添加学员
    :param student:
    :return:
    """
    self.students.append(student)

def remove(self, name: str):
    """
    删除学员
    :param name:
    :return:
    """
    for student in self.students:
        if student.name == name:
            self.students.remove(student)
            break

def show_students(self):
    """
    显式所有的学员信息
    :return:
    """
    for student in self.students:
        print(student)


def average_score(self):
    """
    计算学员的平均成绩
    :return:
    """
    num = len(self.students)
    sum = 0

    for student in self.students:
        sum += student.score

    return sum/num

test_student_manager.py
import pytest
import allure

from student import Student
from student_manager import StudentManager

class TestStudentManager:
pytest.mark.add
def test_add_student(self):
manager = StudentManager()
student1 = Student(“Bob”, 85)
student2 = Student(“Alice”, 80)
manager.add(student1)
manager.add(student2)
assert len(manager .students) == 2

    pytest.mark.remove
    def test_remove(self):
        manager = StudentManager()
        student = Student("Bob", 85)
        manager.add(student)
        manager.remove("Bob")
        assert len(manager.students) == 0

    pytest.mark.average
    def test_average_score(self):
        manager = StudentManager()
        student1 = Student("LiHua", 90)
        student2 = Student("HanMeiMei", 98)
        manager.add(student1)
        manager.add(student2)
        assert manager.average_score() == 94

202041412220 区世钧
学生系统各项功能:




}MUHGE}NKMF72W)_0D__R8
1P{6W@RH7(V`WB_TV_N``H

姓名:王冠毅 学号 :202041412222

student.py

class Student:
    def __init__(self,name,score):
        self.name=name
        self.score=score

    def str(self):
        return f"学生姓名:{self.name},学生成绩是:{self.score}"

student_manager.py

from student import Student


class  StudentManager:
    def __init__(self):
        self.student_list=[]

    def add(self,name,score):
        student=Student(name=name,score=score)
        self.student_list.append(student)
        return student
    def remove(self,name: str):
        for student in self.student_list:
            if student.name==name:
                self.student_list.remove(student)
                return student.name
                break

        return "123"

    def show_list(self):

        for item in self.student_list:
            print(item)

    def AVG_Score(self):
        num=0
        for student in self.student_list:
            num+=student.score

        if num>0:
            AVG=num/len(self.student_list)
            return AVG
        else:
            return 0

test_Student_Manager.py

import allure
import pytest

from student_manager import StudentManager


@allure.feature("学生管理")

class Test_StuManager:
    @allure.story("添加学生")
    @pytest.mark.parametrize("name,score", [("李嘉图", 34), ("陈晖洁", 24)])
    def test_add_Stu(self,name,score):
        manager=StudentManager()

        student_add=manager.add(name=name,score=score)
        assert student_add.name==name


    @allure.story("测试删除")
    @pytest.mark.parametrize("name,score",[("李嘉图", 34),("陈晖洁", 24)])
    def test_remove(self,name,score):
        manager=StudentManager()
        manager.add(name,score)

        name_re=manager.remove(name)
        assert name_re==name

    @allure.story("测试平均")
    def test_AVG(self):
        manager=StudentManager()
        manager.add("小明",12)
        manager.add("小红",18)
        avg_num=manager.AVG_Score()
        assert avg_num==15

allure

image-20231012223332215

202041412210 李侨广 - 作业2


image



202041404213 刘建升



3
4

202041404202陈杰选
student.py

class Student:

    def __init__(self, name, score):
        self.name = name
        self.score = score

student_manager.py

class StudentManager:
    def __init__(self):
        self.students = []

    def add_student(self, student):
        self.students.append(student)

    def remove_student(self, name):
        for student in self.students:
            if student.name == name:
                self.students.remove(student)
                return True
        return False

    def show_student(self, students):
        for student in self.students:
            print(student)

    def calc_avg_score(self, students):
        scores, avg = 0, 0
        for student in self.students:
            scores += student.score
        avg = scores / len(self.students)
        return avg

test_student_manager.py

import pytest

from day2.student import Student
from day2.student_manager import StudentManager


class TestStudentManager:
    def setup_class(self):
        self.student1 = Student("one", 10)
        self.student2 = Student("two", 20)
        self.student_manager = StudentManager()

    def test_add_student(self):
        self.student_manager.add_student(self.student1)
        self.student_manager.add_student(self.student2)
        actual1 = self.student1 in self.student_manager.students
        actual2 = self.student2 in self.student_manager.students
        assert actual1 == True and actual2 == True

    def test_remove_student(self):
        self.student_manager.remove_student(self.student2.name)
        actual = self.student2 in self.student_manager.students
        assert actual == False

    def test_show_student(self):
        self.student_manager.show_student(self.student_manager.students)
        actual = self.student1 in self.student_manager.students
        assert actual == True

    def test_calc_avg_score(self):
        actual = self.student_manager.calc_avg_score(self.student_manager.students)
        assert actual == 10




image
1697620671666

202041412128张艺贤

黄枝沛202041404206


Screenshot_2023-10-19-08-54-03-489_com.tencent.mm
Screenshot_2023-10-19-08-54-13-896_com.tencent.mm