Basic use of unittest automated testing framework for python interface automated testing

Directory

A brief introduction to unittest

unittest basic use

unittest.Testcase

setUp

tearDown

setUpClass

tearDownClass

test case

unittest. main()

Various assertion methods provided by unitteest

unittest test case skip execution

There are four ways to write skip execution test cases

self. skipTest(reason)

Points to note about skipping test cases


A brief introduction to unittest

  • unit testing framework
  • It can also be applied to the development and execution of WEB automated test cases
  • Provides rich assertion methods
  • Official documentation:

unittest basic use

 1 # import unittest module
 2 import unittest
 3
 4
 5 # Create a unit test class, inheriting unittest.TestCase
 6 class testCase(unittest.TestCase):
 7
 8 def setUp(self):
 9 print("before case execution")
10
11 def tearDown(self):
12 print("after case execution")
13
14 @classmethod
15 def setUpClass(cls):
16 print("Before the object is executed")
17
18 @classmethod
19 def tearDownClass(cls):
20 print("After the object is executed")
twenty one 
22 # test case
23 def test_01(self):
24 print("test01")
25
26 def test_02(self):
27 print("test02")
28
29
30 if __name__ == '__main__':
31 unittest. main()

Run results

 1 object before execution
 2 case before execution
 3 test01
 4 case after execution
 5 cases before execution
 6 test02
 7 case after execution
 8 After the object is executed
 9 
10
11 Ran 2 tests in 0.002s
12
13 OK

unittest automated testing framework: 3-day proficiency in Unittest automated testing practical training camp, zero-based beginners can learn_哔哩哔哩_bilibiliicon-default.png?t=N4N7 https://www.bilibili.com/video/BV1zg4y1g7it/?spm_id_from=333.999.0.0

Knowledge points included here:

unittest. Testcase

  • All unit test classes you create must inherit from it, which is the base class for all unit test classes

setUp

  • Used for initialization work before each test case is executed
  • The input parameter of all methods in the class is self, and the definition of instance variables also needs to be self. variable

tearDown

  • This method will be executed after each test case is executed

setUpClass

  • Call this method before each unit test class runs, and it will only be executed once
  • Belongs to class method, need to add decorator @classmethod
  • The default input parameter is cls, which refers to the “class object” itself. In fact, it is no different from self, and the usage is consistent

tearDownClass

  • This method is called after each unit test class is run, and it will only be executed once
  • Belongs to class method, need to add decorator @classmethod

Test case

  • A method whose name must start with “test_”, otherwise it cannot be recognized and executed
  • There needs to be an assertion in the method in order to have the execution result of the use case at the last run
  • Can contain multiple test cases

unittest. main()

  • run unit tests
  • This command will search for all test cases starting with test under the current module and run them
  • The order of execution is named according to the case

Various assertion methods provided by unitteest

 1 class testCase(unittest.TestCase):
 2 
 3 def test_03(self):
 4 # Assertion - Is it True
 5 flag = True
 6 self.assertTrue(flag, msg="Test failure information, you can leave it blank")
 7
 8 def test_04(self):
 9 # Assertion - Is it False
10 flag = False
11 self.assertFalse(flag)
12
13 def test_05(self):
14 # Assertion - Whether the two parameters provided are the same (any type)
15 self.assertEqual("123", "123") # string
16 self.assertEqual({"a": 1}, {"a": 1}) # dictionary
17 self.assertEqual([1, 2], [1, 2]) # list
18 self.assertEqual((1, 2), (1, 2)) # tuple
19 self.assertEqual({1, 2}, {1, 2}) # set
20
21 def test_06(self):
22 # Assertion - whether the lists are the same
23 self.assertListtEqual([1, 2], [1, 2])
twenty four 
25 def test_07(self):
26 # Assertion - Are dictionaries the same
27 self.assertDictEqual({"a": 1}, {"a": 1})
28
29 def test_08(self):
30 # Assertion - Are the tuples the same
31 self. assertTupleEqual((1, 2), (1, 2))
32
33 def test_09(self):
34 # Assertion - Are the collections the same
35 self.assertSetEqual({1, 2}, {1, 2})

This is a relatively common assertion method. Of course, there are some assertion methods that are easier to understand. I have not given examples one by one. For details, please see the following list

method

Equivalent to the writing in python

assertEqual(a, b)

a == b

assertNotEqual(a, b)

a != b

assertTrue(x)

bool(x) is True

assertFalse(x)

bool(x) is False

assertIs(a, b)

a is b

assertIsNot(a, b)

a is not b

assertIsNone(x)

x is None

assertIsNotNone(x)

x is not None

assertIn(a, b)

a in b

assertNotIn(a, b)

a not in b

assertIsInstance(a, b)

isinstance(a, b)

assertNotIsInstance(a, b)

not isinstance(a, b)

assertRegex(s, r)

r. search(s)

unittest automated testing framework:

3-day proficiency in Unittest automation testing practical training camp, zero-based beginners can learn_哔哩哔哩_bilibiliicon-default.png?t=N4N7https://www.bilibili .com/video/BV1zg4y1g7it/?spm_id_from=333.999.0.0

unittest test case skip execution

 1 class testCase(unittest.TestCase):
 2 
 3 # Skip directly
 4 @unittest.skip("skip directly")
 5 def test_skip(self):
 6 self. fail("shouldn't happen")
 7
 8 # skip if the condition is met
 9 @unittest.skipIf(1 < 2, "Skip if the condition is met")
10 def test_skipIf(self):
11 print("skip if")
12
13 # Skip if the condition is not met
14 @unittest.skipUnless(sys.platform.startswith("win"), "The window platform will not be skipped")
15 def test_skipUnless(self):
16 print("skip Unless")
17
18 # The test case is expected to fail
19 @unittest. expectedFailure
20 def test_fail(self):
21 self. assertEqual(1, 0, "broken")
twenty two 
23 # Jump out of the method body without executing the case
24 def test_maybe_skipped(self):
25 if True:
26 self.skipTest("Call the skipTest of unittest, and skip the case if certain conditions are met in the method body")
27 pass

Run results

 1 Skipped: Call the skipTest of unittest, and skip the case if certain conditions are met in the method body
 2 
 3 Skipped: Skip directly
 4
 5 Skipped: skip if the condition is met
 6 skip Unless
 7
 8 
 9 Ran 5 tests in 0.010s
10
11 OK (skipped=3, expected failures=1)

There are four ways to write skip execution test cases

  • @unittest.skip(reason) : skip the test case, reason is the reason why the test is skipped
  • @unittest.skipIf(condition, reason) : When condition is true, skip the test case.
  • @unittest.skipUnless(condition, reason) : skip the test case unless condition is true
  • @unittest.expectedFailure : Mark the test case as expected to fail; if the test fails, it is considered a test success; if the test passes, it is considered a test failure

self. skipTest(reason)

Execution of the test case is skipped only if certain conditions are met in the method body

Skip execution test case points

  • setUp() and tearDown() of skipped tests will not be run
  • Just enter unittest.skip , it can also be skipped normally, no need to write reason
  • If you enter unittest.skip() , reason must be written in the brackets and cannot be empty
  • Skip execution can be set for the unit test class level (just add a decorator directly above the class declaration), and all test cases of this unit test class will not be executed
  • setUpClass() and tearDownClass() of skipped classes will not be run
  • When the self.skipTest(reason) method is called in the method body, the test case will still call setUp() and tearDown()

unittest automated testing framework:

3-day proficiency in Unittest automation testing practical training camp, zero-based beginners can learn_哔哩哔哩_bilibiliicon-default.png?t=N4N7https://www.bilibili.com/ video/BV1zg4y1g7it/?spm_id_from=333.999.0.0

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledgePython entry skill treeWeb crawlerSelenium298962 People are studying systematically