mercredi 25 juillet 2018

how to parametrize a function for pytest such that parameters and test name are taken from a text file

how to parametrize a function for pytest such that parameter are taken from a text file and function name changes on each iteration for pytest-html report

text file format : Function_name | assert_value | Query for assertion from postgresql.

requirement : to create a pytest based framework

so far with my logic(it doesn't work):

with open("F_Query.txt","r") as ins1:
    for F_Query in ins1:
        #Name of function to be executed to be extracted from the file differentiated with delimeter " | "(leading and trailing space required)
        F_name=F_Query.split(" | ")[0]
        assert_val=F_Query.split(" | ")[1]
        Query=F_Query.split(" | ")[2]
        Loc_file=(Query.split(" ")[-5:])[0]
        def f(text):
            def r(y):
                return y
            r.__name__ = text
            return r
            c1.execute(Query)
            assert(c1.rowcount() == assert_val), "Please check output file for records"
        p = f(F_name)

can anyone explain how to get the function name changed with every iteration while parameters are being passed in pytest fun

latest changes (still doesn't work):
with open("F_Query.txt","r") as ins1:
    for F_Query in ins1:
        #Name of function to be executed to be extracted from the file differentiated with delimeter " | "(leading and trailing space required)
        #F_name=F_Query.split(" | ")[0]
        assert_val=int(F_Query.split(" | ")[1])
        Query=F_Query.split(" | ")[2]
        Query=Query.strip("\n")
        #Loc_file=(Query.split(" ")[-5:])[0]
        dictionary1[Query]=assert_val


@pytest.mark.parametrize('argumentname',dictionary1)
def test_values(argumentname):
    c1.execute(dictionary1.keys(argumentname))
    assert(c1.rowcount==dictionary1.values(argumentname))

Aucun commentaire:

Enregistrer un commentaire