3

I need to test a workflow where all of the pages are in the same order, but where the user starts isn't known at the time of testing. All of the individual pages have separate functions with parameters. They run independently of each other and I'd like to keep it that way

I thought that I could store all the functions in a list and then pass a variable to indicate where to start in the list, but when I define the list, it just runs the functions! Any ideas how to fix this?

This is as far as I got before figuring out the functions were running

teststeps = [page1(param1, param2), page2(param1, param2), page3(param1, param2), page4(param1, param2
teststepnumber = determinepage(param1, param2)
for item in teststeps[teststepnumber:]:
**Incomplete because I couldn't figure out how to run the functions**
Brandy
  • 155
  • 3
  • 16
  • 1
    Are you looking for this: https://stackoverflow.com/a/63330003/5431791 ? – Sayandip Dutta Aug 10 '20 at 16:10
  • Does this answer your question? [Python - Store function with parameters](https://stackoverflow.com/questions/10692602/python-store-function-with-parameters) – mkrieger1 Aug 10 '20 at 16:35
  • @SayandipDutta - I'm not using the map function. Thanks – Brandy Aug 10 '20 at 16:53
  • @mkrieger1 - No, I am using the functions in a list as shown above. Thanks – Brandy Aug 10 '20 at 16:53
  • But what you are doing is not working. The answers to the question I have linked show you how you can do it correctly. Don’t get distracted by Tkinter and buttons in the question. – mkrieger1 Aug 10 '20 at 16:56

3 Answers3

2

You need to just put the functions themselves (not the results of actually calling them) in the list so that you can call them in a later step:

test_steps = [page1, page2, page3, page4]
test_step_number = determine_page(param1, param2)
for test_step in test_steps[test_step_number:]:
    test_step(param1, param2)
Samwise
  • 68,105
  • 3
  • 30
  • 44
2

Yes, you can store your functions and their parameters in a list of tuples:

teststeps = [(page1,param1, param2), (page2,param1, param2), (page3,param1, param2), (page4,param1, param2)]
teststepnumber = determinepage(param1, param2)
for item in teststeps[teststepnumber:]:
    f,*args = item
    f(*args)

The line: f,*args = item gives you the function f and all the arguments *args

Then the line f(*args) calls the function f with the appropriate number of arguments.

quamrana
  • 37,849
  • 12
  • 53
  • 71
2

The functions are running immediately when you define the list because they are being called with (). One way to fix this would be a to make a list of tuples where the first item is the function and the second is a list of arguments.

test_funcs = [(page1, [param1, param2]), (page2, [param1, param2])]
for test_func in test_funcs:
    f, args = test_func
    f(*args)
rurp
  • 1,376
  • 2
  • 14
  • 22