当从具有相同inpu的终端运行时,Pytest测试用例失败,但给出了正确的结果

2024-09-27 21:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是pytest的新手,并使用它对python代码进行单元测试。 我在名为difficulty的文件中有一个名为ProblemSorting的类。类中有一个方法get_difficulty_order()

下面是我为测试方法编写的单元测试:

import pytest
from difficulty import ProblemSorting

@pytest.mark.parametrize("num_of_problems, num_of_subtasks, problems, result",
                         [
                             pytest.param(3, 3, [[16, 24, 60],
                                                 [498, 861, 589],
                                                 [14, 24, 62],
                                                 [72, 557, 819],
                                                 [16, 15, 69],
                                                 [435, 779, 232]], [2, 1, 3]),
                             pytest.param(1, 1, [[2], [5]], [1]),
                             pytest.param(0, 0, [], [])])
def test_get_difficulty_order(num_of_problems, num_of_subtasks, problems, result):
    '''hello'''
    prob_sort = ProblemSorting(num_of_problems, num_of_subtasks, problems)
    assert prob_sort.get_difficulty_order() == result

现在的问题是2nd测试用例失败了。但是当我删除1st3rd测试用例并保留2nd测试用例时,它就工作了。即使我用2nd输入手动检查代码,它也会给出预期的结果

注意:我还注意到,不管有多少个测试用例,它只会成功第一个和最后一个测试用例,而中间的所有测试用例都会失败

编辑:难度.py “检查”

import operator

class ProblemSorting:
    '''Class implementing various methods to solve the problem
       which requires ordering of problem based on its difficulty level
    '''

    # stores the tuple (i, difficulty) where 'i' is the problem number
    # and 'difficulty' is the difficulty level associated with the 'i'th problem
    difficulty_of_problems = list()

    def __init__(self, num_of_problems, num_of_subtasks, problems):
        self.num_of_problems = num_of_problems
        self.num_of_subtasks = num_of_subtasks
        self.problems = problems

我不明白这是怎么回事。 任何帮助都将不胜感激


Tags: oftheimportselfgetpytest测试用例order

热门问题