Curtis Banks
Curtis Banks

Reputation: 362

how to test two json file with pytest in python3.6

what would be the best way to test the case below with pytest with python3(3.6}+)?

json_data_one = {
   "id": 1,
   "status": True,
   "name": "Jhon"
}

json_data_two = {
   "id": 2,
   "status": False,
   "name": "Dave"
}
def foo(json_data_one, json_data_two):
    # not the best way to show this
    # but i want to loop through each json data then
    # show the result based on `status` is either `True` or `False`
    if json_data_one["status"] == True:
        return json_data_one
    elif json_data_two["status"] == False:
        return json_data_two

@pytest.mark.parametrize("status, name", [(True, "John"), (False, "dave")])
def test_foo(status, name):
    assert foo(status) == name

The above generate the error

status = True, name = 'John'

    @pytest.mark.parametrize("status, name", [(True, "John"), (False, "dave")])
    def test_foo(status, name):
>       assert foo(status) == name
E    TypeError: foo() missing 1 required positional argument: 'json_data_two'

test_start.py:46: TypeError
___________________________________________________________________________________________ test_foo[False-dave] ___________________________________________________________________________________________

status = False, name = 'dave'

    @pytest.mark.parametrize("status, name", [(True, "John"), (False, "dave")])
    def test_foo(status, name):
>       assert foo(status) == name
E    TypeError: foo() missing 1 required positional argument: 'json_data_two'

test_start.py:46: TypeError
========================================================================================= short test summary info ==========================================================================================
FAILED test_start.py::test_foo[True-John] - TypeError: foo() missing 1 required positional argument: 'json_data_two'
FAILED test_start.py::test_foo[False-dave] - TypeError: foo() missing 1 required positional argument: 'json_data_two'

I am a bit lost on how to implement it but what i want to accomplish is check each json data, then if status == True return "name" == "John" but if status == False return "name" == "dave"

I believe parametrize could be used but i am having a hard time figuring it out.

Thank you.

Upvotes: 1

Views: 1946

Answers (1)

MrBean Bremen
MrBean Bremen

Reputation: 16815

First, your actual function should probably look something like this:

def foo(json_data):
    if json_data["status"]:
        return json_data["name"]
    elif:
        return "Nope"

I don't know what you actually want to do, but the function as is does not make sense. You have to substitute your actual function, of course.

Then your test could look like this:

@pytest.mark.parametrize("data, result", [(json_data_one, "John"), 
                                          (json_data_two, "Nope")])
def test_foo(data, result):
    assert foo(data) == result

Again, your actual result is certainly something else, but I didn't understand what you are trying to do, so you have to adapt this.

Upvotes: 2

Related Questions