Python Forum
pytest loop through multiple tests?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pytest loop through multiple tests?
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 2,901 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Pytest mocks anthonyrmoss78 0 427 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,576 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple Loop Statements in a Variable Dexty 1 1,176 May-23-2022, 08:53 AM
Last Post: bowlofred
  Multiple user defined plots with secondary axes using for loop maltp 1 1,393 Apr-30-2022, 10:19 AM
Last Post: maltp
  Ran 0 tests in 0.000s - unittest Peaches 8 4,931 Dec-31-2021, 08:58 AM
Last Post: Peaches
  Trying to separate a loop across multiple threads stylingpat 0 1,653 May-05-2021, 05:21 PM
Last Post: stylingpat
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,845 Dec-02-2020, 07:50 AM
Last Post: Gilush
  VSCode not able to discover tests rpk2006 5 10,714 Jul-15-2020, 06:03 AM
Last Post: ndc85430
  Multiple MEAN value using FOR loop kka_anand 2 2,121 Jul-14-2020, 06:20 PM
Last Post: kka_anand

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020