Hi Guys,
I`m trying to learn unit testing with the unittest libary.
I have created this modules:
unit_testing
-To me it seems the unit test is executed, although I do not call it. -> Why?
-To me it seems, that if __name__ == "__main__": is not entered as it should -> Why?
-My aim is, to be capable of let the single test modules run in verbose mode, and also together @ unit testing module. But: Not twice (e.g. unwanted run while import). What would be a correct way?
-does the architecture of the whole stuff make sense to you? Are there more elegant ways?
Thank you a lot for helping!
I`m trying to learn unit testing with the unittest libary.
I have created this modules:
- unit_testing -> the "master"
- test_demo_math_module
- test_demo_physics_module
- demo_math_module
- demo_physics_module
unit_testing
#Imports import unittest import test_demo_math_module import test_demo_physics_module #create the master suite master_suite = unittest.TestSuite() #add sub suites master_suite.addTest(test_demo_math_module.suite()) #add a suite object (created by the suite() function) master_suite.addTest(test_demo_physics_module.suite()) #run the master suite unittest.TextTestRunner(verbosity=2).run(master_suite)demo_math_module
def my_cool_add(a: int, b: int): c = a + b return c def my_cool_multiply(a: int, b: int): c = a * b return ctest_demo_math_module
""" demo module for developing unit test feature started 11/2022 General Documentation: https://docs.python.org/2/library/unittest.html https://docs.python.org/3.10/library/unittest.html#unittest.TestProgram https://docs.python.org/3/library/unittest.html#setupmodule-and-teardownmodule """ #Imports import unittest import demo_math_module as math class Test_Math_Stuff(unittest.TestCase): #creating a test case ( basic buliding block / individual unit of testing) def setUp(self): self.pi = 3.14 self.information = "I´m setting up stuff first, after this the tests below will run" def test_my_cool_add(self): #creating a individual test result = math.my_cool_add(1,1) self.assertEqual(result,2) def test_my_cool_multiply(self): # creating a individual test result = math.my_cool_multiply(4, 4) self.assertEqual(result, 16) def tearDown(self): self.information = "I will destroy the stuff setted up (e.g. widgets, windows)" class Test_Other_Stuff(unittest.TestCase): pass #for the ease of running tests, as we will see later, it is a good idea to provide in each test module a callable object that returns a pre-built test suite: def suite(): suite = unittest.TestLoader().loadTestsFromTestCase(Test_Math_Stuff) #adds all Tests from the regarding instance to the suite return suite if __name__ == "__main__": if True: unittest.TextTestRunner(verbosity=2).run(suite()) # run the testNow I let test_demo_math_module run in slightly different variants, only changing the last 3 lines:
if __name__ == "__main__": if [b]True[/b]: unittest.TextTestRunner(verbosity=2).run(suite()) # run the test
Output:C:\Users\spg9ba\.conda\envs\pydrogen\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.2\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path U:/031_Python/040_Git_Repository/rbpydrogen/rbpydrogen/UnitTesting/test_demo_math_module.py
Testing started at 10:56 ...
Launching unittests with arguments python -m unittest U:/031_Python/040_Git_Repository/rbpydrogen/rbpydrogen/UnitTesting/test_demo_math_module.py in U:\031_Python\040_Git_Repository\rbpydrogen\rbpydrogen\UnitTesting
Ran 2 tests in 0.010s
OK
Process finished with exit code 0
if __name__ == "__main__": if [b]False[/b]: unittest.TextTestRunner(verbosity=2).run(suite()) # run the test
Output:C:\Users\spg9ba\.conda\envs\pydrogen\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.2\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path U:/031_Python/040_Git_Repository/rbpydrogen/rbpydrogen/UnitTesting/test_demo_math_module.py
Testing started at 10:58 ...
Launching unittests with arguments python -m unittest U:/031_Python/040_Git_Repository/rbpydrogen/rbpydrogen/UnitTesting/test_demo_math_module.py in U:\031_Python\040_Git_Repository\rbpydrogen\rbpydrogen\UnitTesting
Ran 2 tests in 0.072s
OK
Process finished with exit code 0
#if __name__ == "__main__": #if True: unittest.TextTestRunner(verbosity=2).run(suite()) # run the test
Output:test_my_cool_add (test_demo_math_module.Test_Math_Stuff) ... ok
test_my_cool_multiply (test_demo_math_module.Test_Math_Stuff) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Ran 2 tests in 0.004s
OK
My Questions:-To me it seems the unit test is executed, although I do not call it. -> Why?
-To me it seems, that if __name__ == "__main__": is not entered as it should -> Why?
-My aim is, to be capable of let the single test modules run in verbose mode, and also together @ unit testing module. But: Not twice (e.g. unwanted run while import). What would be a correct way?
-does the architecture of the whole stuff make sense to you? Are there more elegant ways?
Thank you a lot for helping!
