我试图在断言错误python中跳过一些案例

2024-04-28 03:34:59 发布

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

在终端上使用命令py.test ./Combinatory_testing.py -v,以便能够在py.test中运行它 当我运行它时,由于函数“is\u valid\u composition”,它给了我失败的测试,但问题是我希望断言总是通过,而只是跳过is\u valid\u composition案例,而不是将其添加到测试中

你知道我做错了什么吗

#importing libraries
from allpairspy import AllPairs
import pytest


#parameters are mainly:
#Computer names
# OS
# Network to be connected to these PC's
# Contract type for the employers

parameters = [
    ["HP", "DELL"],
    ["98", "NT", "2000", "XP"],
    ["Internal", "Modem"],
    ["Salaried", "Hourly", "Part-Time", "Contr."],
]

#Uncomment below to print all the possible Combinations with less numbers for sure using the function AllPairs

# print("PAIRS:")
# for i, pairs in enumerate(AllPairs(parameters)):
#    print(i,pairs)
#


def is_valid_combination(brand, operating_system, network, contract):
    """
    This is a filtering function. Filtering functions should return True
    if combination is valid and False otherwise.
    """

    # Brand DELL does not support Windows 98
    if "98" == operating_system and "DELL" == brand:
        return False

    # HP does not work with XP
    if "XP" == operating_system and "HP" == brand:
        return False

    # Contractors can't work on "Internal" network.
    if "Contr." == contract and network == "Internal":
        return False

    return True


#Uncomment below to print all the possible Combinations with less numbers for sure using the function AllPairs After Filtering the
#impossible combinations

#
#print("PAIRWISE:")
# for i, pairs in enumerate(AllPairs(parameters, filter_func=is_valid_combination)):
#     print("{:2d}: {}".format(i, pairs))

def function_to_be_tested(brand, operating_system, Network, Contract):
    return True

class TestParameterized(object):
    @pytest.mark.parametrize(["brand", "operating_system", "network", "contract"], [
        value_list for value_list in AllPairs([
            ["HP", "DELL"],
            ["98", "NT", "2000", "XP"],
            ["Internal", "Modem"],
            ["Salaried", "Hourly", "Part-Time", "Contr."],
        ])
    ])

    def test_assert(self,brand,operating_system,network,contract):
        assert is_valid_combination(brand,operating_system,network,contract)

Tags: thetoforreturnisoperatingnetworksystem
1条回答
网友
1楼 · 发布于 2024-04-28 03:34:59

您忘了提到代码示例是从库allpairspy:https://github.com/thombashi/allpairspy中获取和改编的。这个库从作为输入的列表中生成特定的组合。但是,您已经从对AllPairs的调用中删除了命名参数filter_func=is_valid_combination。如果我正确地理解了您的问题,那么再次添加这个将确保只生成这样的组合,您的断言将在其中传递

相关问题 更多 >