selenium自学之路:unittest单元测试框架

单元测试本身就是通过一段代码验证另一段代码,所以不用单元测试也可以写单元测试
例如
calculator.py

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

    def __add__(self):
        return self.a+self.b

写一段程序来验证它

from calculator import Count
class TestCount:
    def test_add(self):
        try:
            j=Count(2,3)
            add=j.add()
            assert(add ==5),'Integer assition result error!'
        except AssertionError as msg:
            print(msg)
        else:
            print('Test pass!')

mytest=TestCount()
mytest.test_add()

接下来使用unittest来验证

from calculator import Count
import unittest
class TestCount(unittest.TestCase):
    def setUp(self):
        print('test start')

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

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


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

你可能感兴趣的:(unittest单元测试框架,selenium)