PYTEST
Tags: beginners, python, programming, testing
Published: 2023-06-15 15:53:41

Hi there! I need your help guys!
Here's my code, where I can't write a test with pytest:
def get_input_values():
    global P_man, density, L1, L2, E_mud, ID_casing, thickness_casing
    P_man, density, L1, L2, E_mud, ID_casing, thickness_casing = (0, 0, 0, 0, 0, 0, 0)
    while True:
        try:
            if SU == "si":
                while P_man <= 0:
                    P_man = float(input("Input Wellhead Pressure, MPa: "))
                    if P_man <= 0:
                        raise ValueError("Pressure must be greater than 0 Pa")
                while density <= 0:
                    density = float(input("Input Mud Density, kg/m3: "))
                    if density <= 0:
                        raise ValueError("Density must be greater than 0 kg/m3")
                while L1 <= 0:
                    L1 = float(input("Input head of liner-hanger depth, m: "))
                    if L1 <= 0:
                        raise ValueError(
                            "Head of liner-hanger depth inputted incorrect, it must be greater than 0 m"
                        )
                while L2 <= 0 or (L2 < L1):
                    L2 = float(input("Input depth of casing shoe, m: "))
                    if L2 <= 0 or (L2 < L1):
                        raise ValueError(
                            "The depth of casing shoe must be deeper than liner-hanger depth"
                        )
                while E_mud <= 0:
                    E_mud = float(input("Input the mud's elastic modulus, Pa: "))
                    if E_mud <= 0:
                        raise ValueError("Fluid modulus must be greater than 0 Pa")
                while ID_casing <= 0:
                    ID_casing = float(input("Input casing's inner diameter, mm: "))
                    if ID_casing <= 0:
                        raise ValueError("Inner diameter must be greater than 0 mm")
                while thickness_casing <= 0 or (thickness_casing > ID_casing):
                    thickness_casing = float(input("Input casing's thickness, mm: "))
                    if thickness_casing <= 0 or (thickness_casing > ID_casing):
                        raise ValueError(
                            "Wall thickness of casing must be greater than 0 mm and less than inner diameter of casing"
                        )
            elif SU == "usc":
                while P_man <= 0:
                    P_man = float(input("Input Wellhead Pressure, psi: "))
                    P_man = P_man * 0.00689476
                    if P_man <= 0:
                        raise ValueError("Pressure must be greater than 0 psi")
                while density <= 0:
                    density = float(input("Input Mud Density, lbm/gal: "))
                    density = density * 119.826
                    if density <= 0:
                        raise ValueError("Density must be greater than 0 kg/m3")
                while L1 <= 0:
                    L1 = float(input("Input head of liner-hanger depth, ft: "))
                    L1 = L1 * 0.3048
                    if L1 <= 0:
                        raise ValueError(
                            "Head of liner-hanger depth inputted incorrect, it must be greater than 0 ft"
                        )
                while L2 <= 0 or (L2 < L1):
                    L2 = float(input("Input depth of casing shoe, ft: "))
                    L2 = L2 * 0.3048
                    if L2 <= 0 or (L2 < L1):
                        raise ValueError(
                            "The depth of casing shoe must be deeper than liner-hanger depth"
                        )
                while E_mud <= 0:
                    E_mud = float(input("Input the mud's elastic modulus, psi: "))
                    E_mud = E_mud * 6894.76
                    if E_mud <= 0:
                        raise ValueError("Fluid modulus must be greater than 0 psi")
                while ID_casing <= 0:
                    ID_casing = float(input("Input casing's inner diameter, inch: "))
                    ID_casing = ID_casing * 25.4
                    if ID_casing <= 0:
                        raise ValueError("Inner diameter must be greater than 0 inch")
                while thickness_casing <= 0 or (thickness_casing > ID_casing):
                    thickness_casing = float(input("Input casing's thickness, inch: "))
                    thickness_casing = thickness_casing * 25.4
                    if thickness_casing <= 0 or (thickness_casing > ID_casing):
                        raise ValueError(
                            "Wall thickness of casing must be greater than 0 mm and less than inner diameter of casing"
                        )
            return P_man, density, L1, L2, E_mud, ID_casing, thickness_casing
        except ValueError as e:
            if "could not convert string to float" in str(e):
                print("Wrong data! You must input numerical value.")
            else:
                print(e)

my test is:
def test_get_input_values_correct_values_si(monkeypatch):
    SU = 'si'
    user_input = ['27', '2180', '3000', '3500', '3004', '108.6', '9.19']
    input_values = iter(user_input)
    monkeypatch.setattr('builtins.input', lambda _: next(input_values))
    expected_result = (
        float(user_input[0]),
        float(user_input[1]),
        float(user_input[2]),
        float(user_input[3]),
        float(user_input[4]),
        float(user_input[5]),
        float(user_input[6])
    )
    assert expected_result == get_input_values()

but it doesnt work...