How can I iterate though all of the functions in a specific module from my current place of execution?
Here is some detail on what I've done thus far:
I've created a package called "test_cases" that has a module, "test_cases" which holds dozens of functions. I'd like to create some kind of loop that executes every function in the test_case module.
Below the first line in main()
is test_cases.test_cases.tc_1()
this is just to test that my script can call on a specific function and it works. Its the "looping" aspect that is troubling me.
Here is the rough idea of my code:
def main():
# test_cases.test_cases.tc_1()
for _, test in test_cases.test_cases:
test
I've read this post but the answer suggests to use for name, val in my_module.__dict__.iteritems():
however iteritems()
is not an attribute of __dict__
in python 3.8 according to PEP 469 (assuming I've read that right). I've also read this post but it is still not clear. So I am a little perplexed on what to do.
I'm running Windows 10, PyCharm2020.1.2, Python 3.8
EDIT 1
Here is my new main()
function with items()
used as the iterator. When debugging, it seems to step through the __name__, __doc__, __package__
etc elements (what are those called?) of my test_case
module. It then identifies my function, tc_1
and attempts to execute it but nothing happens. I would expect it to run the print()
statement. I've attached my function below for reference. Whats going on here?
def main():
for name, test in test_cases.test_cases.__dict__.items():
if callable(test):
test
# tc_1 is in the test_cases module
def tc_1():
print("TEST CASE ONE EXECUTED")
EDIT 2
The final issue was not including the parenthesis after test
. Once this was corrected the loop works as expected. Here is the code for reference:
def main():
for name, test in test_cases.test_cases.__dict__.items():
if callable(test):
test()