The Selenium (sixteen): unittest unit testing framework (II)

1. Recognizes unittest (cont.)

About unittest unit testing framework, there are some issues worthy of further exploration. You may have some doubts in the learning process in the previous chapter, you might find the answer in this section.

Example 1.1 with the execution sequence

The execution order of use cases involve multiple levels, in the case of multiple test directory, which directory to perform? In the case of multiple test files, which file to execute? In the case of multiple test classes, which test to perform first class? In the case of multiple test methods (use cases), and which test method to execute?

Let's run an example, again explain the implementation of the policy unittest.

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_bbb(self):
        print("test bbb")

    def tearDown(self):
        pass

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

operation result:

 

No matter how many times the execution, the result is the same, the result of the above, I believe you have found the law unittest test case execution.

unittest test frame loaded by default according to the order of ASCII code, as the order of numbers and letters: 0-9, AZ, az. Therefore, testAdd priority class is performed based on TestBdd, test_aaa () method will be limited to 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 enforce it? The answer is yes, but you can not use the default main () method, but rather need to addTest TestSuite class () to load in a certain order.

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_bbb(self):
        print("test bbb")

    def tearDown(self):
        pass

IF  the __name__ == ' __main__ ' :
     # configured test set 
    Suite = unittest.TestSuite ()
    suite.addTest(TestBdd("test_ccc"))
    suite.addTest(TestAdd("test_bbb"))
    suite.addTest(TestBdd("test_aaa"))

    # Perform tests 
    Runner = unittest.TextTestRunner ()
    runner.run(suite)

operation result:

 

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

1.2 Example performed with a multi-level directory

We have to control the number of Web use cases, but when the order reaches a certain test cases, we should consider partitioning the directory.

test_project/test_case/

  test_bbb /

    test_ccc/

      test_c.py

    test_b.py

  test_ddd/

    test_d.py

  test_a.py

For the above directory structure, if the parameter is defined start_dir discover () method is "./test_case/" directory, you can only load test_a.py file test cases. How to make unittest framework looks to test_case / subdirectory high-end test documents? The method is very simple, to place a __init__.py file in each subdirectory (that contains a __init __ in the folder. Py, Python will put the folder as a package, inside the py file import can be on the outside).

1.3 skip the test and is expected to fail

When you run the test, and sometimes need to skip some of the test cases, or skip the test when patients met with a condition, or directly to the test cases to fail. unittest provides an implementation of these requirements decorator.

unittest.skip (reason): unconditional skip the modified test, the test is skipped reasons.

unittest.skipIf (condition, reason): Skip the modified test, if the condition is true.

unittest.skipUnless (condition, reason): Skip modified test, unless the condition is true.

unittest.expectedFailure (): the test is marked as failed. Regardless of whether the results of the failure, unity marked as failed.

import unittest

class MyTest(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    unittest.skip @ ( " skip test " )
     DEF test_skip (Self):
         Print ( ' Test AAA ' )

    unittest.skipIf @ ( . 3> 2, " when the condition is True, skip the test " )
     DEF test_skip_if (Self):
         Print ( ' Test BBB ' )

    unittest.skipUnless @ ( . 3> 2, " when the execution condition is True Test " )
     DEF test_skip_unless (Self):
         Print ( ' Test CCC ' )

    @unittest.expectedFailure
    def test_expected_failure(self):
        assertEqual(2,3)


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

operation result:

 

The example CCP created four test cases. The first test by @ unittest.skip () modified skip is not performed. By using the second embodiment @ unittest.skipIf () modification, when the execution condition is not true, 3> 2 condition is true (True), by not performing. The third condition is true, when executed, determines 3> 2 condition is true (True), the third use case performed by @ unittest.skipUnless decoration, when. Article by @ unittest.expectedFailure decorative use cases, regardless of whether the result of failure to perform, collectively labeled as a failure, but does not throw an error message.

Of course, these methods may also be applied to the test classes can simply define them in the test above category.

import unittest

@unittest.skip("直接跳过测试该测试类")
class MyTest(unittest.TestCase):

1.4 fixtures

fixtures的概念前面已经有过简单的介绍,可以形象的把它看作是夹心饼干外层的两片饼干,这两片饼干就是setUp/tearDown,中间的心就是测试用例。除此之外,unittest还提供了更大范围的fixtures,例如对于测试类和模块的fixtures。

import unittest

def setUpModule():
    print("test module start >>>>>>>>>>>>>>>>")

def tearDownModule():
    print("test module end >>>>>>>>>>>>>>>>")

class Test(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("test class start ==============>")

    @classmethod
    def setDownClass(cls):
        print("test class end ==============>")

    def setUp(self):
        print("test case start -->")

    def tearDown(self):
        print("test case end -->")

    def test_case(self):
        print("test case1")

    def test_case2(self):
        print("test case2")

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

运行结果:

 

setUpModule/tearDownModule:在整个模块的开始与结束时被执行。

setUpClass/tearDownClass:在测试类的开始与结束时被执行。

setUp/tearDown:在测试用例的开始与结束时被执行。 

需要注意的是, setUpClass/tearDownClass的写法稍微有些不同。首先,需要通过@classmethon进行修饰,其次方法的参数为cls。其实,cls与self并没有什么特别之处,都只表示类方法的第一个参数,只是大家约定俗成,习惯于这样来命名,当然也可以用abc来代替。

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11972047.html