【问题标题】:python: What am I doing wrong here in my testing code?python:我在测试代码中做错了什么?
【发布时间】:2020-03-16 13:48:33
【问题描述】:

我想了解我在这里做错了什么。

from enum import Enum, auto
class colors(Enum):
    red= auto()
    green= auto()
    yellow= auto()

这是我的课。

     def is_first(self):
        return self is not colors.red

我的第一个函数。

    def second(self):
        if self is colors.red:
            return ''
        elif self is green:
            return 'second_type'
        elif self is yellow:
            return 'third_type'

我在测试中做错了什么我需要他们都通过。

     @pytest.mark.parametrize('input_str, expected_result',
                    [('aa', False)])

    def test_is_first(input_str, expected_result):
        assert is_first(input_str) is expected_result

对于我的第二个功能

    @pytest.mark.parametrize('input_str, expected_result',
                    [('', True),
            ('second_type', True),
            ('third_type', True),
            ('aa', False)])

    def test_second(input_str, expected_result):
        assert second(input_str) is expected_result

【问题讨论】:

    标签: python testing enums auto


    【解决方案1】:
    @pytest.mark.parametrize('input_str, expected_result',
                    [('', True),
            ('second_type', True),
            ('third_type', True),
            ('aa', False)])
    
    def test_second(input_str, expected_result):
        assert second(input_str) is expected_result
    

    你写道:

    def second(self):
        if self is colors.red:
            return ''
        elif self is green:
            return 'second_type'
        elif self is yellow:
            return 'third_type'
    

    这将通过测试:

    def second(self):
        if self == '':
            return True
        elif self == 'second_type':
            return True
        elif self == 'third_type':
            return True
        elif self 'aa':
            return False
    

    你的assert is_first(input_str) is expected_result 会失败。

    def is_first(self):
      return self is not colors.red
    

    这将返回TrueFalse。您的 expected_result 正在根据字符串检查布尔值。

    【讨论】:

    • 我调整了答案。您应该查找基本的 pytest 模板。你仍然遗漏了很多代码。因此,仅复制粘贴不会解决您的问题。在应用测试之前,您应该了解测试的实际工作原理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2016-02-17
    相关资源
    最近更新 更多