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:
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
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/a6ec34cb-9096-4a9c-9667-08552c3b68df%40aliyun.com.
For more options, visit https://groups.google.com/d/optout.