[TIP] How to create my unittest scripts for my directory structure?(No attachment)

Ned Batchelder ned at nedbatchelder.com
Thu Apr 2 04:13:38 PDT 2015


Hi Yao,

You are doing way more work than you need to do.  You don't need to put 
each test case in a separate directory.  You don't need to write 
runTest() ever.  You don't need to build TestSuites ever.  You don't 
need to write a test.py script that calls unittest, just use "unittest 
discover" directly from the command line.

Can you explain why you want runTest, TestSuites, and test.py?  What 
will they do for you that unittest doesn't already do?

--Ned.

On 4/1/15 11:26 PM, yaoyansibase wrote:
>
>
> Hi all,
> I read the python unittest document, but I still have some problems. 
> Could I ask some questions? Because I don't know how to create my 
> unittest scripts for my directory structure.
> My directory structure looks like this(I tended to attach the 
> directory structure and my scripts for your easy test, but the 
> email has been trapped and automatically deleted due to its 
> attachments, so I have to delete my attachement and re-send my email 
> again):
>
> project_0001/
>  |___plugin_0001/
>  |    |___test/
>  |    |     |___testcase_0001/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0002/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0003/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___ __init__.py
>  |    |     |___ test.py
>  |    |
>  |    |_____ __init__.py
>  |    |_____ test.py
>  |
>  |___plugin_0002/
>  |    |___test/
>  |    |     |___testcase_0001/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0002/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0003/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___ __init__.py
>  |    |     |___ test.py
>  |    |
>  |    |_____ __init__.py
>  |    |_____ test.py
>  |
>  |___plugin_0003/
>  |    |___test/
>  |    |     |___testcase_0001/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0002/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___testcase_0003/
>  |    |     |     |___ __init__.py
>  |    |     |     |___ test.py
>  |    |     |
>  |    |     |___ __init__.py
>  |    |     |___ test.py
>  |    |
>  |    |_____ __init__.py
>  |    |_____ test.py
>  |
>  |____ __init__.py
>  |____ test.py
>
>
>
> My requirements and my problems:
> I tend to write only one testcase in each testcase 
> diretory(e.g.project_*/plugin_*/test/testcase_*/test.py)
>   For example,
> ##### project_0001\plugin_0001\test\test_0001\test.py #####
> import unittest
> class TestStringMethods(unittest.TestCase):
>     def setUp(self):
>         print('\nthis is test_0001.TestStringMethods.setUp');
>
>     def tearDown(self):
>         print('\nthis is test_0001.TestStringMethods.tearDown');
>
>     def test_upper(self):
>         self.assertEqual('foo'.upper(), 'FOO')
>         #print('this file is: '+__file__);
>         print('\nthis is test_0001.TestStringMethods.test_upper');
>
>     def test_isupper(self):
>         self.assertTrue('FOO'.isupper())
>         self.assertFalse('Foo'.isupper())
>         print('\nthis is test_0001.TestStringMethods.test_isupper');
>
>     def runTest(self):
>         print('\nthis is test_0001.TestStringMethods.runTest');
>
> if __name__ == '__main__':
> #    unittest.main()
>
>     suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
>     unittest.TextTestRunner(verbosity=2).run(suite)
> ###########################################################
>
> Then I run the test
> \>cd project_0001\plugin_0001\test
> \>python -m unittest discover
>
> And here is the log:
> this is test_0001.TestStringMethods.setUp
> this is test_0001.TestStringMethods.test_isupper
> this is test_0001.TestStringMethods.tearDown
> .
> this is test_0001.TestStringMethods.setUp
> this is test_0001.TestStringMethods.test_upper
> this is test_0001.TestStringMethods.tearDown
> .this is test_0002.TestStringMethods.test_A
> .this is test_0002.TestStringMethods.test_B
> .this is test_0003.TestStringMethods.test_isupper
> .this file is: \project_0001\plugin_0001\test\test_0003\test.pyc
> this is test_0003.TestStringMethods.test_upper
> .
> ----------------------------------------------------------------------
> Ran 6 tests in 0.000s
> OK
>
>
>
> Question 1:
> why test_0001.TestStringMethods.runTest() is not called? how to invoke 
> runTest()?
> Should I write my test code in test_0001.TestStringMethods.test_*() or 
> test_0001.TestStringMethods.runTest()?
> Which one is better?
>
>
> Question 2:
> How to do 'python -m unittest discover' in python script? I create my 
> test script but failed.
> Here is my steps:
>
> First, I write my script project_0001\plugin_0001\test/test.py
> #####   project_0001\plugin_0001\test/test.py   #####
> import os
> import sys
> import unittest
>
> def main ():
>     print('__file__='+__file__);
>     start_dir = os.path.dirname(os. path.abspath(__file__));
>     start_dir = start_dir.replace('\\', '/')
>     print('start_dir='+start_dir);
>
>     unittest.defaultTestLoader.discover(start_dir, pattern='test_*')
>
> if __name__ == '__main__':
>     main ()
> #####################################################
>
> Second, I run the test
> \>cd project_0001\plugin_0001\test
> \>python ./test.py
>
> And here is the log:
> __file__=./test.py
> start_dir=D:/dev/mymagicbox/automation/test/project_0001/plugin_0001/test
>
> It shows my problem, all the testcase_*/test.py are not run. Why? how 
> to solve this problem?
>
>
>
>
> Question 3:
> 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:
>     project_0001\plugin_0001\test\testcase_0001\,
>     project_0001\plugin_0001\test\testcase_0002\,
>     project_0001\plugin_0001\test\testcase_0003\,
> But, sometimes, I tend to skip testcase_0002 and only do unittest for 
> testcase_0001 and testcase_0003.
> How to deal with this problem?
>
>
> Question 4:
> 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 :
>     project_0001\plugin_0001\test\testcase_0001\,
>     project_0001\plugin_0001\test\testcase_0002\,
>     project_0001\plugin_0001\test\testcase_0003\,
>     project_0001\plugin_0002\test\testcase_0001\,
>     project_0001\plugin_0002\test\testcase_0002\,
>     project_0001\plugin_0002\test\testcase_0003\,
>     project_0001\plugin_0003\test\testcase_0001\,
>     project_0001\plugin_0003\test\testcase_0002\,
>     project_0001\plugin_0003\test\testcase_0003\,
> 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:
>     project_0001\plugin_0001\test\testcase_0001\,
>     project_0001\plugin_0001\test\testcase_0002\,
>     project_0001\plugin_0001\test\testcase_0003\,
>     project_0001\plugin_0003\test\testcase_0001\,
>     project_0001\plugin_0003\test\testcase_0002\,
>     project_0001\plugin_0003\test\testcase_0003\,
> How to deal with this problem?
>
>
>
> Question 5:
> 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?
>
>
> Finally, any suggestion is appreciated, and thank you in advance.
>
> Cheers
> yao
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20150402/8d4b12b3/attachment.html>


More information about the testing-in-python mailing list