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


更新2020/03/23

在网上看了一个视频说nose默认只能执行以test开头的用例,这又一次让我怀疑自己,再次回来温习这编文章,发现有误解读者的意思,特更新下

发现的规则默认正则表达式是这样的:

(?:^|[\b_\.\-])[Tt]est

?:  --- 表示不分组的
^: --- 表示开头
| --- 或
\b_: ----单词和_
\.和\-: ---- 在python函数不支持这二个符号,所以这里意义不大 所以从上面这段正则中可以看出,匹配的是以Test或test开头的、有单词加_的;我们试试
def test_aa():
pass def test_bb():
pass def testff():
pass def aa_test():
pass def aatest():
pass def aatest_aa():
pass

执行结果如下:

[root@ecs-x-medium--linux- bbb]# nosetests  -v -s test_qq.py
test_qq.test_aa ... ok
test_qq.test_bb ... ok
test_qq.testff ... ok
test_qq.aa_test ... ok ----------------------------------------------------------------------
Ran tests in .001s

所以网上说的nose只以能test开头或test结果的说法都是错误。应该这么说,以大写T或小写t开头的、或[T|t]est前面带有_的用例都会执行

python nose测试框架全面介绍十一---用例的发现的更多相关文章

  1. python nose测试框架全面介绍十---用例的跳过

    又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...

  2. python nose测试框架全面介绍七--日志相关

    引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...

  3. python nose测试框架全面介绍六--框架函数别名

    之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...

  4. python nose测试框架全面介绍五--attr介绍

    之前写了一系列nose框架的,这篇介绍下attr tag 在nose框架中attr用来标识用例,使得在运行时可以通过标识来执行用例,之前在nose测试框架全面介绍四中有说明,但没有说明清楚,这里再总结 ...

  5. python nose测试框架全面介绍一

    一.简介      nose 是python自带框架unttest的扩展,使测试更简单高效:nose是一个开源的项目,可以在官网上下载源码 1.快速安装 有以下几中安装方式: easy_install ...

  6. python nose测试框架全面介绍四

    四.内部插件介绍 1.Attrib 标记,用于筛选用例 在很多时候,用例可以分不同的等级来运行,在nose中很增加了这个功能,使用attrib将用例进行划分 有两种方式: ef test_big_do ...

  7. python nose测试框架全面介绍三

    三.nose的测试工具集 nose.tools模块提供了一系列的小工具,包括测试执行时间.异常输出及unittest框架中所有的assert功能. 为了使写用例更加容易,nose.tools提供了部分 ...

  8. python nose测试框架全面介绍二

    二.基本使用 nosetest脚本的使用(在安装完nose之后) nosetests [options] [(optional) test files or directories] 我们可以使用配置 ...

  9. python nose测试框架全面介绍十二 ----用例执行顺序打乱

    在实际执行自动化测试时,发现我们的用例在使用同一个资源的操作时,用例的执行顺序对测试结果有影响,在手工测试时是完全没法覆盖的. 但每一次都是按用例名字来执行,怎么打乱来执行的. 在网上看到一个有意思的 ...

随机推荐

  1. Vue相关开源项目库汇总(史上最全)

    偶尔在网上看到的,先保存着,以备不时之需 https://www.jianshu.com/p/e37f4dde49a2 better-scroll:是一款重点解决移动端(未来可能会考虑 PC 端)各种 ...

  2. pygame 笔记-4 代码封装&发射子弹

    继续之前的内容,随着游戏的内容越来越复杂,有必要把代码优化一下,可以参考OOP的做法,把人物类抽象出来,弄成一个单独的类,这们便于代码维护,同时我们给小人儿,加个发射子弹的功能,代码如下:(看上去略长 ...

  3. Unity项目中文字的统一管理

    一款游戏在研发初期就需要考虑多语言的问题,否则后期在进行多国语言版本时就面临着巨大的成本.鉴于之前页游的经验,其它同事设计出读取Excel的方式来管理所有的文字.但是我在使用中发现很致使的一个问题,当 ...

  4. mysql批量修改列名为小写

    --  bo_project_info 为表名 SELECT concat( 'alter table ', 'bo_project_info', ' change column ', COLUMN_ ...

  5. Canvas 和 SVG 的不同

    Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的. SVG SVG 是一种使用 XML 描述 2D 图形的语言. SVG 基于 XML,这意味着 SVG DOM 中的每个 ...

  6. hadoop ha 读取 activce状态的活动节点

    方式一 package com.xxx.hadoop; import com.sun.demo.jvmti.hprof.Tracker; import com.sun.xml.bind.util.Wh ...

  7. Eclipse创建第一个Spring Boot项目

    一.安装SpringBoot插件 安装过程需要联网下载插件,属于在线安装,请耐心等待安装完成,下载安装完成以后,需要重启Eclipse 二.创建Spring Boot项目 如下图所示new-other ...

  8. 【ASP.NET Core】浅说目录浏览

    何谓“浅说”?就是一句话说不完,顶多两句话就介绍完毕,然后直接给上实例的解说方式.化繁为简,从七千年前到现在,从老祖宗到咱们,一直都在追求的理想目标,尽可能把复杂的东西变成简单的. 老周告诉你一个可以 ...

  9. Webdings字体和Wingdings字体对照表

    一.Webdings是一个TrueType的dingbat字体,于1997年发表,搭载在其后的Microsoft Windows视窗系统内,大多的字形都没有Unicode的相对字. 使用很简单1.网页 ...

  10. mysql字符串用法

    replace(str,from_str,to_str) --用字符串to_str替换字符串str中的子串from_str并返回 --mysql> select replace('www.mys ...