Selenium 2 test automation combat 30 (unittest supplement)

unittest supplement

1. Example execution sequence

# test.py
#coding:utf-8
from Demo1 import Count
import unittest

class TestBdd(unittest.TestCase):

    def setUp(self):
        print "test TestBdd:"

    def test_ccc(self):
        print "test ccc"

    def test_aaa(self):
        print "test aaa"

    def tearDown(self):
        pass

class TestAdd(unittest.TestCase):
    def setUp(self):
        print "test TestAdd:"

    def test_add(self):
        print "test bbb"

    def tearDown(self):
        pass

if __name__=="__main__":
    unittest.main() 

Result of operation as shown below:

 

 

No matter how many times the execution, the result is the same, by the above results, the following law:
the unittest test frame loaded by default according to the order of the ASCII code, the order of numbers and letters: 0 ~ 9, A ~ Z , az. Therefore, testAdd priority class is performed based on TestBdd, test_aaa () method takes precedence test_ccc () is executed, so it is not performed in the order from top to bottom with the embodiment.

For the test directory and test files for, unittest framework is also in accordance with this rule to load test cases.
So can you let test_ccc () to perform? The answer is yes, but you can not use the default main () method, but rather a need for methods to load in a certain order by addTest TestSuite class ().

# test.py
#coding:utf-8
import unittest

class TestBdd(unittest.TestCase):

    def setUp(self):
        print "test TestBdd:"

    def test_ccc(self):
        print "test ccc"

    def test_aaa(self):
        print "test aaa"

    def tearDown(self):
        pass

class TestAdd(unittest.TestCase):
    def setUp(self):
        print "test TestAdd:"

    def test_add(self):
        print "test bbb"

    def tearDown(self):
        pass

if __name__=="__main__":
    suite=unittest.TestSuite()
    suite.addTest(TestBdd("test_ccc"))
    suite.addTest(TestAdd("test_add"))
    suite.addTest(TestBdd("test_aaa"))

    runner=unittest.TextTestRunner()
    runner.run(suite)

Execution results are as follows:

Now the execution order is the order addTest () method loaded. The Discover () loading test rule with main () in the same manner. So, we can only increase the priority to be executed by the name of the test case. For example: you will want to first test cases to be executed named "test_a", hope that the final execution of the test case named "test_z".

 

2. Do the use cases of multi-level directory

To control the number of cases with a web, but when the test reaches a certain magnitude, it is necessary to consider dividing the directory, such as directory test directory:
test_project / test_case /
--test_bbb /
------ test_ccc /
----- ----test_c.py
------test_b.py
--test_ddd /
---------test_d.py
test_a.py
for this directory, if discover () start_dir positioning method parameter as "./test_case/" directory, you can only load test_a.py file test cases. How to make unittest framework looks to test file test_case / subdirectory of it? The method is very simple, to place a __init__.py file in each directory.

 

Guess you like

Origin www.cnblogs.com/Rita-LJ/p/11778099.html