1. 程式人生 > >python nose測試框架全面介紹十一---用例的發現

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,不要看網上,相信自己的真實看到的就行