python - Pytest 测试用例失败,但从具有相同输入的终端运行时给出正确结果
问题描述
我是 pytest 的新手,并使用它对我的 python 代码进行单元测试。我ProblemSorting
在文件中有一个名为difficulty
. 类中有一个方法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
测试用例失败了。但是当我删除1st
和3rd
测试用例并保留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
我不明白这有什么问题。任何帮助将不胜感激。
解决方案
推荐阅读
- php - Symfony 4 PHP Exception Doctrine\ORM\ORMException: "Unknown Entity namespace ali as 'User'。"
- reactjs - Netlfy React 构建失败,但它在本地工作
- c# - 删除 DataTable 表头
- sql - 使用 CTE 中另一个表中不存在的列
- r - 将 fabeltools 包降级到 0.1.3。R
- java - 如何在 JavaFX 中将目录路径与 TreeItem 绑定?
- javascript - jQuery load() 有 200 个响应代码,但所需内容未加载到所选元素中
- java - Struts2 中的动作映射
- orocommerce - 我需要帮助将 OroCommerce 升级到 4.1.1
- c# - 是否有无头 Chrome 的替代品来使用扩展程序?