python nose测试框架全面介绍十一---用例的发现

nose是怎么发现用例的??网上一大把说函数以test开头的都会自动发现,真的是这样吗???还是自己来试验下吧

 首先,我们还是来看看官方文档怎么说的吧:

If it looks like a test, it’s a test. Names of directories, modules, classes and functions are compared against the testMatch regular expression, and those that match are considered tests. Any class that is a unittest.TestCase subclass is also collected, so long as it is inside of a module that looks like a test.

Files with the executable bit set are ignored by default under Unix-style operating systems–use --exe to allow collection from them, but be careful that is safe to do so. Under Windows, executable files will be picked up by default since there is no executable bit to test.

Directories that don’t look like tests and aren’t packages are not inspected.

Packages are always inspected, but they are only collected if they look like tests. This means that you can include your tests inside of your packages (somepackage/tests) and nose will collect the tests without running package code inappropriately.

When a project appears to have library and test code organized into separate directories, library directories are examined first.

When nose imports a module, it adds that module’s directory to sys.path; when the module is inside of a package, like package.module, it will be loaded as package.module and the directory of package will be added to sys.path.

If an object defines a __test__ attribute that does not evaluate to True, that object will not be collected, nor will any objects it contains.

什么意思呢?

就是说,

1、查找,只找目录,模块、类及函数,还有以unittest.TestCase继承的子类

2、可执行文件也会查看

3、不找非包形式的目录

来看看,testMatch正则是什么呢,我们来看看nosetests -h

 -m REGEX, --match=REGEX, --testmatch=REGEX
                       Files, directories, function names, and class names
                       that match this regular expression are considered
                       tests.  Default: (?:^|[\b_\.\-])[Tt]est

从这段可以看出,默认的nose,不是仅仅匹配test开头的,而是包含test字样的文件,文件夹,类名或函数名。

我们来举个例子

#coding:utf-8
'''
Created on 2017年11月1日
@author: huzq
'''

from unitest_class_for_nose import yy
class TestClass():
    
    def setUp(self):
        print "MyTestClass setup"

    def tearDown(self):
        print "MyTestClass teardown"
        
    def Testfunc1(self):
        print "this is Testfunc1"
        pass
    
    def test_func1(self):
        print "this is test_func1"
        pass 
    
    def Testfunc2(self):
        print "this is Testfunc2"
        pass 
    
    def test_func2(self):
        print "this is test_func2"
        pass

    def aaa_test(self):
        print "xxxx"
    
    def test_io(self):
        
        yy().test_yh()
        pass

class afdfdTest():
    def test_aaa(self):
        pass

这样一个简单的例子,

默认打出来的是这样的:

nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
test_case.nose_learn_45.TestClass.Testfunc1 ... ok
test_case.nose_learn_45.TestClass.Testfunc2 ... ok
test_case.nose_learn_45.TestClass.aaa_test ... ok
test_case.nose_learn_45.TestClass.test_func1 ... ok
test_case.nose_learn_45.TestClass.test_func2 ... ok
test_case.nose_learn_45.TestClass.test_io ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.003s

OK

连aaa_test也出来了,但class afdfdTest里的测试却没有。

再改改脚本,将afdfdTest继承unittest.TestCase

from unittest import TestCase
...

class afdfdTest(TestCase):
    def test_aaa(self):
        pass

再看看执行:

nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
test_case.nose_learn_45.TestClass.Testfunc1 ... ok
test_case.nose_learn_45.TestClass.Testfunc2 ... ok
test_case.nose_learn_45.TestClass.aaa_test ... ok
test_case.nose_learn_45.TestClass.test_func1 ... ok
test_case.nose_learn_45.TestClass.test_func2 ... ok
test_case.nose_learn_45.TestClass.test_io ... ok
test_aaa (test_case.nose_learn_45.afdfdTest) ... ok

----------------------------------------------------------------------
Ran 7 tests in 0.005s

OK

这下出来了。

如果你想真只执行以test开头的脚本该怎么做呢?,如下

nosetests -v -s --match="^[Tt]est" nose_learn_45.py --collect-only


nose.config: INFO: Ignoring files matching ['^\\.', '^_', '^setup\\.py$']
test_case.nose_learn_45.TestClass.Testfunc1 ... ok
test_case.nose_learn_45.TestClass.Testfunc2 ... ok
test_case.nose_learn_45.TestClass.test_func1 ... ok
test_case.nose_learn_45.TestClass.test_func2 ... ok
test_case.nose_learn_45.TestClass.test_io ... ok
test_aaa (test_case.nose_learn_45.afdfdTest) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.004s

OK

可以看出aaa_test没有出来了。。。。

需要注意的是 --match后面的一定要双引号

So,不要看网上,相信自己的真实看到的就行

猜你喜欢

转载自www.cnblogs.com/landhu/p/10079478.html
今日推荐