I'm doing a simple script to run and test my code. How can i import dinamically and run my test classes?
- I'm not sure what you're asking here. If you've got a solution, then you can answer it yourself by posting it first as a question.Makoto– Makoto2015-07-28 16:42:12 +00:00Commented Jul 28, 2015 at 16:42
- Welcome to SO! I'm sorry, but I don't understand your question. Can you clarify? What are the expected results? What are you seeing?AlG– AlG2015-07-28 16:43:28 +00:00Commented Jul 28, 2015 at 16:43
- Sorry @AIG. I hope I have clarified the idea.Heros– Heros2015-07-28 16:52:17 +00:00Commented Jul 28, 2015 at 16:52
- Did you read the documentation?Burhan Khalid– Burhan Khalid2015-07-29 11:59:33 +00:00Commented Jul 29, 2015 at 11:59
3 Answers
This is the solution that I found to import and dynamically run my test classes.
import glob import os import imp import unittest def execute_all_tests(tests_folder): test_file_strings = glob.glob(os.path.join(tests_folder, 'test_*.py')) suites = [] for test in test_file_strings: mod_name, file_ext = os.path.splitext(os.path.split(test)[-1]) py_mod = imp.load_source(mod_name, test) suites.append(unittest.defaultTestLoader.loadTestsFromModule(py_mod)) text_runner = unittest.TextTestRunner().run(unittest.TestSuite(suites)) 1 Comment
Install pytest, and run your tests with a command like:
py.test src That's it. Py.test will load all test_*.py files, find all def test_* calls inside them, and run each one for you.
The board is having trouble answering your question because it's in "why is water wet?" territory; all test rigs come with runners that automatically do what your code snip does, so you only need read the tutorial for one to get started.
And major props for writing auto tests at all; they put you above 75% of all programmers.
Comments
This solution is too simple and perform what i want.
import unittest def execute_all_tests(tests_folder): suites = unittest.TestLoader().discover(tests_folder) text_runner = unittest.TextTestRunner().run(suites)