简体   繁体   English

使用Pytest测试条件语句

[英]Testing of Conditional Statement using Pytest

When I run the test, it shows that only 1 test is passed. 当我运行测试时,它表明只有1个测试通过了。 How to the test_function so that it shows that all the tests are passed. 如何使用test_function,以显示所有测试均已通过。

Note that eval() function doesn't take any parameter. 请注意,eval()函数不带任何参数。

import pytest

def eval():
    a=1  #got this value after calling some function (this can be 1,2,3 or any value)
    if a ==2:
        return 8
    elif a == 3:
        return 4
    else:
        return 42

@pytest.mark.parametrize("expected", [
        (8),
        (4),
        (42),
    ])
def test_eval(expected):
    assert eval() == expected

Okay, after clarification in the comments, that a is a global... It would be better if it wasn't. 好吧,在评论中澄清之后, a是一个全局的……如果不是,那会更好。 :) :)

But if you can't change its signature, 但是,如果您不能更改其签名,

import pytest


def eval():
    if a == 2:
        return 8
    elif a == 3:
        return 4
    else:
        return 42


@pytest.mark.parametrize(
    "input_value, expected", [(2, 8), (3, 4), (4, 42)]
)
def test_eval(input_value, expected):
    global a
    a = input_value
    assert eval() == expected

should do the trick for you. 应该为您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM