<div class="__aliyun_email_body_block"><div style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14px;color:#000000;"><br></div><div style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14px;color:#000000;">Hi all,<br>I read the python unittest document, but I still have some problems. Could I ask some questions?<br>Because I don't know how to create my unittest scripts for my directory structure.<br>My directory structure looks like this, and it is attached as a package:<br><br>project_0001/<br>&nbsp;|___plugin_0001/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |___test/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0001/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0002/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0003/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ test.py<br>&nbsp;|<br>&nbsp;|___plugin_0002/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |___test/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0001/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0002/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0003/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ test.py<br>&nbsp;|<br>&nbsp;|___plugin_0003/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |___test/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0001/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0002/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___testcase_0003/<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; |___ test.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ __init__.py<br>&nbsp;|&nbsp;&nbsp;&nbsp; |_____ test.py<br>&nbsp;|<br>&nbsp;|____ __init__.py<br>&nbsp;|____ test.py<br><br><br><br>My requirements and my problems:<br>I tend to write only one testcase in each testcase diretory(e.g.project_*/plugin_*/test/testcase_*/test.py)<br>&nbsp; For example, <br>##### project_0001\plugin_0001\test\test_0001\test.py #####<br>import unittest<br>class TestStringMethods(unittest.TestCase):<br>&nbsp;&nbsp;&nbsp; def setUp(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print('\nthis is test_0001.TestStringMethods.setUp');<br><br>&nbsp;&nbsp;&nbsp; def tearDown(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print('\nthis is test_0001.TestStringMethods.tearDown');<br><br>&nbsp;&nbsp;&nbsp; def test_upper(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.assertEqual('foo'.upper(), 'FOO')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #print('this file is: '+__file__);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print('\nthis is test_0001.TestStringMethods.test_upper');<br><br>&nbsp;&nbsp;&nbsp; def test_isupper(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.assertTrue('FOO'.isupper())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.assertFalse('Foo'.isupper())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print('\nthis is test_0001.TestStringMethods.test_isupper');<br><br>&nbsp;&nbsp;&nbsp; def runTest(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print('\nthis is test_0001.TestStringMethods.runTest');<br><br>if __name__ == '__main__':<br>#&nbsp;&nbsp;&nbsp; unittest.main()<br><br>&nbsp;&nbsp;&nbsp; suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)<br>&nbsp;&nbsp;&nbsp; unittest.TextTestRunner(verbosity=2).run(suite)<br>###########################################################<br><br>Then I run the test <br>\&gt;cd project_0001\plugin_0001\test<br>\&gt;python -m unittest discover<br>&nbsp;<br>And here is the log:<br>this is test_0001.TestStringMethods.setUp<br>this is test_0001.TestStringMethods.test_isupper<br>this is test_0001.TestStringMethods.tearDown<br>.<br>this is test_0001.TestStringMethods.setUp<br>this is test_0001.TestStringMethods.test_upper<br>this is test_0001.TestStringMethods.tearDown<br>.this is test_0002.TestStringMethods.test_A<br>.this is test_0002.TestStringMethods.test_B<br>.this is test_0003.TestStringMethods.test_isupper<br>.this file is: \project_0001\plugin_0001\test\test_0003\test.pyc<br>this is test_0003.TestStringMethods.test_upper<br>.<br>----------------------------------------------------------------------<br>Ran 6 tests in 0.000s<br>OK<br><br><br><br>Question 1:<br>why test_0001.TestStringMethods.runTest() is not called? how to invoke runTest()?<br>Should I write my test code in test_0001.TestStringMethods.test_*() or test_0001.TestStringMethods.runTest()?<br>Which one is better?<br><br><br>Question 2:<br>How to do 'python -m unittest discover' in python script? I create my test script but failed. <br>Here is my steps:<br><br>First, I write my script project_0001\plugin_0001\test/test.py<br>#####&nbsp;&nbsp; project_0001\plugin_0001\test/test.py&nbsp;&nbsp; #####<br>import os<br>import sys<br>import unittest<br><br>def main ():<br>&nbsp;&nbsp;&nbsp; print('__file__='+__file__);<br>&nbsp;&nbsp;&nbsp; start_dir = os.path.dirname(os. path.abspath(__file__));<br>&nbsp;&nbsp;&nbsp; start_dir = start_dir.replace('\\', '/')<br>&nbsp;&nbsp;&nbsp; print('start_dir='+start_dir);<br><br>&nbsp;&nbsp;&nbsp; unittest.defaultTestLoader.discover(start_dir, pattern='test_*')<br><br>if __name__ == '__main__':<br>&nbsp;&nbsp;&nbsp; main ()<br>#####################################################<br><br>Second, I run the test <br>\&gt;cd project_0001\plugin_0001\test<br>\&gt;python ./test.py<br><br>And here is the log:<br>__file__=./test.py<br>start_dir=D:/dev/mymagicbox/automation/test/project_0001/plugin_0001/test<br><br>It shows my problem, all the testcase_*/test.py are not run. Why? how to solve this problem?<br><br><br><br><br>Question 3:<br>Sometimes, I will take project_0001\plugin_0001\test as the start_dir of my unittest. By default, it should recursively run the unittest in the following sub-directories:<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0003\,<br>But, sometimes, I tend to skip testcase_0002 and only do unittest for testcase_0001 and testcase_0003.<br>How to deal with this problem?<br><br><br>Question 4:<br>Sometimes, I will take project_0001\ as the start_dir of my unittest. By default, it should recursively run the unittest for the following sub-directories :<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0003\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0002\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0002\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0002\test\testcase_0003\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0003\,<br>But, sometimes, I tend to skip plugin_0002 and only recursively do unittest for plugin_0001 and plugin_0003. It means doing the following unittests:<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0001\test\testcase_0003\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0001\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0002\,<br>&nbsp;&nbsp; &nbsp;project_0001\plugin_0003\test\testcase_0003\,<br>How to deal with this problem?<br><br><br><br>Question 5:<br>If it would be possible, I tend to invoke these unittest in Maya which is a DCC software(www.autodesk.com/products/maya/overview). Maya has a python script editor, it can run the unittest by invoking execfile(start_dir+'/test.py'), but it complains __file__ is not defined. What should I do? I mean, should I try to avoid using __file__ as possible as I can in my unittest scripts? or should I create my global variable 'g__file__' to replace __file__, and how to do this?<br><br><br>Finally, any suggestion is appreciated, and thank you in advance.<br><br>Cheers<br>yao<br></div><div class="__aliyun_signature_wrap"></div><div style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14px;color:#000000;"><br></div></div>