unitTest框架学习

自动化测试用例管理

  • 传统方式
1
2
3
4
5
6
7
8
9
10
11
12
class Test():
def test1(self):
print(1)
def test2(self):
print(1)
def test3(self):
print(1)

test =Test()
test.test1()
test.test2()
test.test3()
  • unitTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import unittest

#继承unittest.TestCase类
class Test(unittest.TestCase):
def test_1(self):
print(1)
def test_2(self):
print(1)
def test_3(self):
print(1)
def fun_name(self):
print(f'If the function name does not start with test, it will not be executed\n函数名称不是test开头则不会执行')
if __name__ == '__main__':
unittest.main()

执行顺序 num字段 fun_name_num

测试套件

执行指定用例

  1. 创建套件
  2. 添加用例
  3. 执行用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import unittest

#继承unittest.TestCase类
class Test(unittest.TestCase):
def test_1(self):
print(1)
def test_2(self):
print(1)
def test_3(self):
print(1)
if __name__ == '__main__':
#创建套件
suit = unittest.TestSuite()
#添加用例(单个)
suit.addTest(Test('test_3'))
#添加用例(多个)
suit.addTests([Test('test_1'),Test('test_3')])
#执行用例
case = unittest.TextTestRunner()
case.run(suit)

如果存在多个类

  1. 创建套件
  2. 创建load对象加载类
  3. 添加指定的类
  4. 执行类的用例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import unittest

#继承unittest.TestCase类
class Test_1(unittest.TestCase):
def test_1(self):
print(1)
def test_2(self):
print(1)
def test_3(self):
print(1)
class Test_2(unittest.TestCase):
def test_4(self):
print(4)
def test_5(self):
print(5)
def test_6(self):
print(6)
if __name__ == '__main__':
#创建套件
suit = unittest.TestSuite()
#创建load对象加载类
load = unittest.TestLoader()
suit.addTests(load.loasTestsFromTestCase(Test_2))
case = unittest.TextTestRunner()
case.run(suit)

测试固件

setUp(self) tearDown(self) 函数:

  1. 每次执行函数方法时,必须先执行一次 setUp(self)函数,执行完一个函数方法后,必须再执行一次 tearDown(self)函数。
  2. unittest默认根据 ASCII码的顺序加载执行用例,数字与字母的顺序为:0-9A-Za-z。所以以 A开头的测试用例方法会优先执行,以 a开头会后执行。
  3. 当函数方法以 test开头进行读取,不是以 test开头的不执行该方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import unittest

#继承unittest.TestCase类
class Test_1(unittest.TestCase):
def setUp(self):
print('打开浏览器')
def test_1(self):
print(1)
def test_2(self):
print(1)
def test_3(self):
print(1)
def tearDown(self):
print('关闭浏览器')

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

运行结果如下

1
2
3
4
5
6
7
8
9
打开浏览
1
关闭浏览
打开浏览
2
关闭浏览
打开浏览
3
关闭浏览

我们发现,每执行一条用例前后都会去调用 setUptearDown方法,极大地降低了下效率

setUpClass(cls) tearDownClass(cls) 类方法:

  1. 执行函数方法前,先执行一次 setUpClass(cls)里面的方法,执行完函数方法后,再执行一次 tearDownClass(cls)里面的函数方法。
  2. unittest默认根据 ASCII码的顺序加载执行用例,数字与字母的顺序为:0-9A-Za-z。所以以 A开头的测试用例方法会优先执行,以 a开头会后执行。
  3. 当函数方法以 test开头进行读取,不是以 test开头的不执行该方法。
  4. setUpClass(cls)tearDownClass(cls) 并不一定要组合使用,它们是同级关系不是上下级关系,可以单独拆分使用。例如:setUpClass(cls)为前置条件,tearDown(self)为后置条件,那么 setUpClass(cls)需要添加装饰器且里面的方法只会执行一次,tearDown(self)后置条件不用加装饰器,只是每执行完一个函数方法后,都会去执行一次 tearDown(self)函数里面的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import unittest

#继承unittest.TestCase类
class Test_1(unittest.TestCase):

@classmethod #classmethod装饰器
def setUpClass(cls):
print('打开浏览器')
def test_1(self):
print(1)
def test_2(self):
print(1)
def test_3(self):
print(1)
@classmethod #classmethod装饰器
def tearDownClass(cls):
print('关闭浏览器')

if __name__ == '__main__':
unittest.main()
1
2
3
4
5
打开浏览
1
2
3
关闭浏览