How to Run Pytest On If Else Statement?

Advertisements

I successfully run Pytest for calculate_bmi function, but failed to run Pytest for bmi_index function. May I know how to run Pytest for bmi_index function?

def main():
    weight = float(input('Enter weight (kg): '))
    height = float(input('Enter height (cm): '))

    bmi = calculate_bmi(weight, height)
    bmi_index(bmi)
    
def calculate_bmi(weight, height):
    bmi = round(10000 * weight / (height ** 2))
    return bmi

def bmi_index(bmi):
    if bmi <= 18.5:
        print('You are underweight.')
    elif bmi <= 24.9:
        print('You are normal weight.')
    elif bmi <= 29.9:
        print('You are overweight.')
    else: 
        print('You are obese.') 

if __name__ == "__main__":
    main()

I’ve tried the following, but test failed (for test_bmi_index). The error was: AssertionError: assert None == ‘You are obese.’ I think it means there is no test to perform, but I’m not sure how. Can you assist me? Thank you very much.

from practice4 import calculate_bmi, bmi_index
import pytest
from pytest import approx


def test_calculate_bmi():
    """Verify that the calculate bmi function works correctly."""
    assert calculate_bmi(47, 154) == approx(20)
    assert calculate_bmi(-95, 175) == approx(-31)
    assert calculate_bmi(4.5, 1.54) == approx(18975)

def test_bmi_index():
    """Verify that the bmi_index function works correctly."""
    assert bmi_index(31) == 'You are obese.'


pytest.main(["-v", "--tb=line", "-rN", __file__])

========================= test session starts ==========================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- C:\Users\iamsp\AppData\Local\Programs\Python\Python39\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\iamsp\Documents\Intro To Python Development
collected 2 items

test_practice4.py::test_calculate_bmi PASSED                      [ 50%]
test_practice4.py::test_bmi_index FAILED                          [100%]

=============================== FAILURES ===============================
c:\Users\iamsp\Documents\Intro To Python Development\test_practice4.py:14: AssertionError: assert None == 'You are obese.'
===================== 1 failed, 1 passed in 0.06s ======================
PS C:\Users\iamsp\Documents\Intro To Python Development> 

>Solution :

Printing is not the same as returning. The function bmi_index prints text but doesn’t return anything (thus the None return value). You can solve this by adding a return statement:

def bmi_index(bmi):
    if bmi <= 18.5:
        return 'You are underweight.'
    elif bmi <= 24.9:
        return 'You are normal weight.'
    elif bmi <= 29.9:
        return 'You are overweight.'
    else: 
        return 'You are obese.'

or, if you prefer:

def bmi_index(bmi):
    if bmi <= 18.5:
        print('You are underweight.')
        return 'You are underweight.'
    elif bmi <= 24.9:
        print('You are normal weight.')
        return 'You are normal weight.'
    elif bmi <= 29.9:
        print('You are overweight.')
        return 'You are overweight.'
    else: 
        print('You are obese.')
        return 'You are obese.'

Leave a ReplyCancel reply