<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi Yao,<br>
<br>
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.<br>
<br>
Can you explain why you want runTest, TestSuites, and test.py? What
will they do for you that unittest doesn't already do?<br>
<br>
--Ned.<br>
<br>
<div class="moz-cite-prefix">On 4/1/15 11:26 PM, yaoyansibase wrote:<br>
</div>
<blockquote
cite="mid:309a4186-a34e-401e-9ed7-65b0752ed4c9@aliyun.com"
type="cite">
<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;"><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? Because I don't know how
to create my unittest scripts for my directory structure.<br>
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):<br>
<br>
project_0001/<br>
|___plugin_0001/<br>
| |___test/<br>
| | |___testcase_0001/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0002/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0003/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___ __init__.py<br>
| | |___ test.py<br>
| |<br>
| |_____ __init__.py<br>
| |_____ test.py<br>
|<br>
|___plugin_0002/<br>
| |___test/<br>
| | |___testcase_0001/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0002/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0003/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___ __init__.py<br>
| | |___ test.py<br>
| |<br>
| |_____ __init__.py<br>
| |_____ test.py<br>
|<br>
|___plugin_0003/<br>
| |___test/<br>
| | |___testcase_0001/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0002/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___testcase_0003/<br>
| | | |___ __init__.py<br>
| | | |___ test.py<br>
| | |<br>
| | |___ __init__.py<br>
| | |___ test.py<br>
| |<br>
| |_____ __init__.py<br>
| |_____ test.py<br>
|<br>
|____ __init__.py<br>
|____ 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>
For example, <br>
##### project_0001\plugin_0001\test\test_0001\test.py #####<br>
import unittest<br>
class TestStringMethods(unittest.TestCase):<br>
def setUp(self):<br>
print('\nthis is test_0001.TestStringMethods.setUp');<br>
<br>
def tearDown(self):<br>
print('\nthis is
test_0001.TestStringMethods.tearDown');<br>
<br>
def test_upper(self):<br>
self.assertEqual('foo'.upper(), 'FOO')<br>
#print('this file is: '+__file__);<br>
print('\nthis is
test_0001.TestStringMethods.test_upper');<br>
<br>
def test_isupper(self):<br>
self.assertTrue('FOO'.isupper())<br>
self.assertFalse('Foo'.isupper())<br>
print('\nthis is
test_0001.TestStringMethods.test_isupper');<br>
<br>
def runTest(self):<br>
print('\nthis is
test_0001.TestStringMethods.runTest');<br>
<br>
if __name__ == '__main__':<br>
# unittest.main()<br>
<br>
suite =
unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)<br>
unittest.TextTestRunner(verbosity=2).run(suite)<br>
###########################################################<br>
<br>
Then I run the test <br>
\>cd project_0001\plugin_0001\test<br>
\>python -m unittest discover<br>
<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>
##### project_0001\plugin_0001\test/test.py #####<br>
import os<br>
import sys<br>
import unittest<br>
<br>
def main ():<br>
print('__file__='+__file__);<br>
start_dir = os.path.dirname(os. path.abspath(__file__));<br>
start_dir = start_dir.replace('\\', '/')<br>
print('start_dir='+start_dir);<br>
<br>
unittest.defaultTestLoader.discover(start_dir,
pattern='test_*')<br>
<br>
if __name__ == '__main__':<br>
main ()<br>
#####################################################<br>
<br>
Second, I run the test <br>
\>cd project_0001\plugin_0001\test<br>
\>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>
project_0001\plugin_0001\test\testcase_0001\,<br>
project_0001\plugin_0001\test\testcase_0002\,<br>
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>
project_0001\plugin_0001\test\testcase_0001\,<br>
project_0001\plugin_0001\test\testcase_0002\,<br>
project_0001\plugin_0001\test\testcase_0003\,<br>
project_0001\plugin_0002\test\testcase_0001\,<br>
project_0001\plugin_0002\test\testcase_0002\,<br>
project_0001\plugin_0002\test\testcase_0003\,<br>
project_0001\plugin_0003\test\testcase_0001\,<br>
project_0001\plugin_0003\test\testcase_0002\,<br>
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>
project_0001\plugin_0001\test\testcase_0001\,<br>
project_0001\plugin_0001\test\testcase_0002\,<br>
project_0001\plugin_0001\test\testcase_0003\,<br>
project_0001\plugin_0003\test\testcase_0001\,<br>
project_0001\plugin_0003\test\testcase_0002\,<br>
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(<a class="moz-txt-link-abbreviated" href="http://www.autodesk.com/products/maya/overview">www.autodesk.com/products/maya/overview</a>). 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>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
</pre>
</blockquote>
<br>
</body>
</html>