(39%) recognize Unittest unit testing framework unittest- organizational unit test cases

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install any additional information please search on their own here do not introduced, all examples are performed using python3.6 + selenium.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

Organizational unit test cases

 

When we add function tested and the corresponding test cases, test cases look at how unittest unit testing framework and is an extension of the new organization.

 

New File calculator.py file

 

# Calculator
class Count():
    def __init__(self,a,b):
        self.a = int(a)
        self.b = int(b)

    # Calculate addition
    def add(self):
        return self.a + self.b

    # Subtraction calculation
    def add(self):
        return self.a - self.b

 

Because of the calculator (calculator) has added a subtraction functions (sub), we need to write test cases for new features, test.py file extensions as follows.

 

import unittest
from calculator import Count

class TestAdd(unittest.TestCase):
    def setUp(self):
        print('test add start')

    def test_add(self):
        j = Count(2,3)
        self.assertEqual(j.add(),5)

    def test_add2(self):
        j = Count(10,5)
        self.assertEqual(j.add(),15)

    def tearDown(self):
        print('test add end')


class TestSub(unittest.TestCase):
    def setUp(self):
        print('test sub start')

    def test_sub(self):
        j = Count(3, 2)
        self.assertEqual(j.sub(), 1)

    def test_sub2(self):
        j = Count(10,5)
        self.assertEqual(j.sub(), 5)

    def tearDown(self):
        print('test sub end')

# Perform the method of test class
if __name__ == '__main__':
    # Structure test set
    suite = unittest.TestSuite()
    suite.addTest(TestAdd("test_add"))
    suite.addTest(TestAdd("test_add2"))
    suite.addTest(TestSub("test_sub"))
    suite.addTest(TestSub("test_sub2"))

    # Run test set
    runner = unittest.TextTestRunner()
    runner.run(suite)

 

Created testAdd () in the example and the TestSub () test two classes, were tested calculator.py file add () and sub () two functions. () Test Method The test method different classes AddTest TestSuite class assembled by the test suite, the execution results are as follows:

 

Can be seen by the test results, setUp () and tearDown () methods are applied to the beginning and end of each test case. If the same in each class setUp () and tearDown () does something, it is not their own can encapsulate a class test it?

import unittest
from calculator import Count

class MyTest(unittest.TestCase):
    def setUp(self):
        print('test case start')

    def tearDown(self):
        print('test case end')

class TestAdd(MyTest):

    def test_add(self):
        j = Count(2,3)
        self.assertEqual(j.add(),5)

    def test_add2(self):
        j = Count(10,5)
        self.assertEqual(j.add(),15)


class TestSub(MyTest):
    def setUp(self):
        print('test sub start')

    def test_sub(self):
        j = Count(3, 2)
        self.assertEqual(j.sub(), 1)

    def test_sub2(self):
        j = Count(10,5)
        self.assertEqual(j.sub(), 5)

    def tearDown(self):
        print('test sub end')

# Perform the method of test class
if __name__ == '__main__':
    unittest.main()

operation result:

 

Creating MyTest () class of obvious benefit for the test class and test methods, we should focus on the preparation of specific use cases, without concern for setUp () and tearDown (doing things). However, things did when preconditions setUp () and tearDown () is required for each use case.

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11505060.html