nose执行顺序

nose单元测试执行顺序

nose和unittest一样,都可以在用例中指定setUp()tearDown()(用户测试初始化以及测试结束后的操作),在nose中,package、module、class都可以设置setup()和teardown();

package中设置

在package中设置,整个测试的运行期间只会执行一次(新建python package时,会生成一个init.py文件,在其中设置setUp()以及tearDown()即可)

用例中每次都执行setup及teardown

在模块、类中执行顺序示例:
新建Testexec.py文件,内容:

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
26
27
28
29
30
31
32
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: kyle
@time: 2018/2/7 10:39
"""
from nose.plugins.skip import SkipTest


class TestClass():

def setUp(self):
print("=============-My Testcase is setup-===========")

def tearDown(self):
print("============My Testcase is teardown===========")

def test_fun1(self):
print("This is test_fun1*******")
pass

def test_fun2(self):
print("This is test_fun2=====")
pass

def test_fun3(self):
print("This is test_fun3~~~~~~~~~")
raise SkipTest

def test_Fun1(self):
print("This is test_Fun1***====***")
pass

命令行执行nosetests:

1
nosetests -v -s Testexec.py

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{lamb} nosetests -v -s Testexec.py                                               
Testexec.TestClass.test_Fun1 ... =============-My Testcase is setup-===========
This is test_Fun1***====***
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun1 ... =============-My Testcase is setup-===========
This is test_fun1*******
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun2 ... =============-My Testcase is setup-===========
This is test_fun2=====
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun3 ... =============-My Testcase is setup-===========
This is test_fun3~~~~~~~~~
============My Testcase is teardown===========
SKIP

----------------------------------------------------------------------
Ran 4 tests in 0.005s

OK (SKIP=1)

可以看见在对每一个函数进行测试时,都执行了一次setUp()以及tearDown();且用例执行顺序是:

1
test_Fun1 -> test_fun1 -> test_fun2 -> test_fun3

其中test_fun3跳过了测试,执行顺序是按照先大写字母,再小写字母,然后再按阿拉伯数字排列的。

用例中只执行一次setup及teardown

只需要在setUpClass()tearDownClass()前加修饰器@classmethod即可:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: kyle
@time: 2018/2/7 10:39
"""
from nose.plugins.skip import SkipTest


class TestClass():

@classmethod
def setUpClass(cls):
print("只初始化一次setup=======")

@classmethod
def tearDownClass(cls):
print("只teardown一次***********")

def setUp(self):
print("=============-My Testcase is setup-===========")

def tearDown(self):
print("============My Testcase is teardown===========")

def test_fun1(self):
print("This is test_fun1*******")
pass

def test_fun2(self):
print("This is test_fun2=====")
pass

def test_fun3(self):
print("This is test_fun3~~~~~~~~~")
raise SkipTest

def test_Fun1(self):
print("This is test_Fun1***====***")
pass

执行测试:

1
nosetests -v -s Testexec.py

执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{lamb} nosetests -v -s Testexec.py                                                   
只初始化一次setup=======
Testexec.TestClass.test_Fun1 ... =============-My Testcase is setup-===========
This is test_Fun1***====***
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun1 ... =============-My Testcase is setup-===========
This is test_fun1*******
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun2 ... =============-My Testcase is setup-===========
This is test_fun2=====
============My Testcase is teardown===========
ok
Testexec.TestClass.test_fun3 ... =============-My Testcase is setup-===========
This is test_fun3~~~~~~~~~
============My Testcase is teardown===========
SKIP
只teardown一次***********

----------------------------------------------------------------------
Ran 4 tests in 0.006s

OK (SKIP=1)

可以看见,setUpClass()以及tearDownClass都只执行了一次。在自动化测试中,可用来加载配置信息,只需要加载一次即可。

文章目录
  1. package中设置
  2. 用例中每次都执行setup及teardown
  3. 用例中只执行一次setup及teardown
|