nose是怎麼發現用例的??網上一大把說函數以test開頭的都會自動發現,真的是這樣嗎???仍是本身來試驗下吧express
首先,咱們仍是來看看官方文檔怎麼說的吧:app
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.
什麼意思呢?ide
就是說,函數
一、查找,只找目錄,模塊、類及函數,還有以unittest.TestCase繼承的子類測試
二、可執行文件也會查看this
三、不找非包形式的目錄lua
來看看,testMatch正則是什麼呢,咱們來看看nosetests -hspa
-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字樣的文件,文件夾,類名或函數名。code
咱們來舉個例子blog
#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,不要看網上,相信本身的真實看到的就行