Python Forum

Full Version: pytest loop through multiple tests?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to convert some code I have to loop through and check an array of hostnames with pytest. I'm getting a chicken-and-egg problem, where the test function is complaining about values not being a proper value, but the place it would be initialized with a value would be in main, past the function definition and its parametrize decorator is. While this is expected behavior, it's not in the list of hostnames for the use cases I want to run through in the result_for dict. Any thoughts on how I can get it to run and still have pytest run the validation for each the hostnames in result_for?

Specifically, I want to see one dot for each hostname. Currently, the code fails after the one failed test, even before it gets to processing the hostnames in results_for (see code below).

##########################################################
<snip> initialization of parameters ... <snip>
retval = "" # Dummy value, to be assigned later
hostname = ""
@pytest.mark.parametrize("result_for, flds_ref, cfg, results, retval",\
        [ (result_for, flds_ref, cfg, results, retval) ])
def test_GetAppFromHostname( result_for, flds_ref, cfg, results, retval) :
    assert dt.hostname.GetAppFromHostname(hostname, flds_ref,
                cfg, results) == retval

if __name__ == '__main__':
    hostindex = 0
    for hostname, retval in result_for.items() :
        hostindex = hostindex +1
        details = {"id" : hostindex, "hostname" : hostname, "errs" : [],
                "valid_hostname" : "yes", "breakdown" : []}
        results["details"].append(details)
        test_GetAppFromHostname( result_for, flds_ref, cfg, results )
    dtv.dtvlog.EndLog(start_time, results)
    quit()