简体   繁体   中英

Testing of Conditional Statement using Pytest

When I run the test, it shows that only 1 test is passed. How to the test_function so that it shows that all the tests are passed.

Note that eval() function doesn't take any parameter.

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. :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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