【原】linux系统中,执行nosetests无法遍历以“test”开头的目录或文件的问题

最近在centos下面安装使用了nose,发现了一个问题:在命令行中执行nosetests无法遍历该目录下面以“test”开头的目录或者文件并执行文件中的测试

1 # nosetests -v
2 
3 ----------------------------------------------------------------------
4 Ran 0 tests in 0.000s
5 
6 OK

而在windows下面是可以的:

1 nosetests -v 
2 here ... ok
3 test1.test4 ... ok
4 
5 ----------------------------------------------------------------------
6 Ran 2 tests in 0.003s
7 
8 OK
 1 #我执行nosetests -v命令的目录下面文件结构
 2 #--test.py
 3 #--test/
 4 #         test1.py
 5 
 6 #test.py
 7 def test():
 8     '''here '''
 9     print 'ok'
10 
11 #test1.py
12 def test():
13     print 'ok'
我执行nosetests -v命令的目录下面文件结构

 

在网上查找了好久,也没找到这是为什么以及怎么解决这个问题,最后,一个前辈让我回归到nose本身的参数去找,执行nosetests -h发现了如下的两个参数:

1 --exe                 Look for tests in python modules that are executable.
2                         Normal behavior is to exclude executable modules,
3                         since they may not be import-safe [NOSE_INCLUDE_EXE]
4 --noexe               DO NOT look for tests in python modules that are
5                         executable.(The default on the windows platform is to
6                         do so.)

--exe后面的注释大致就是说:使用这个参数的话,nose会去python模块中寻找可执行的测试用例,一般情况下是去把那些可执行的模块包含进来,尽管它们或许不能被安全地导入……而--noexe后面的注释正好与前面的相反,尤其是其中的“windows platform”让我觉得,这两个参数可能就是解决问题的关键,于是执行了下面的命令:

1 [root@A test]# nosetests -v --exe
2 here ... ok
3 test.test4 ... ok
4 
5 ----------------------------------------------------------------------
6 Ran 2 tests in 0.003s
7 
8 OK

果然就行了,心中大喜!至于为什么nose要这样设计,我想是:nose认为导入那些默认可以执行的文件可能会对系统照成破坏吧。

因为现在我的目录下各个文件的权限是这样的:

1 [root@A test]# ll
2 总用量 12
3 drwxr-xr-x 2 root root 4096 12月 31 23:16 test1
4 -rwxr--r-- 1 root root   88 1月   6 04:15 test.py
5 -rw-r--r-- 1 root root  264 1月   6 04:15 test.pyc

可以看出test.py是有执行权限的,通过chmod -x test.py将执行权限去掉后可以正常执行了

 1 [root@A test]# nosetests -v
 2 
 3 ----------------------------------------------------------------------
 4 Ran 0 tests in 0.000s                  #没有测试被运行
 5 
 6 OK
 7 [root@A test]# chmod -x test.py  #去掉test.py的执行权限
 8 [root@A test]# nosetests -v
 9 here ... ok
10 
11 ----------------------------------------------------------------------
12 Ran 1 test in 0.001s                     #此时只运行了test.py并没有运行test1.py因为前面只去掉了test.py的执行权限
13 
14 OK
15 [root@A test]# ll
16 总用量 12
17 drwxr-xr-x 2 root root 4096 12月 31 23:16 test1
18 -rw-r--r-- 1 root root   88 1月   6 04:15 test.py
19 -rw-r--r-- 1 root root  264 1月   6 04:15 test.pyc

所以在linux下面有两种方式可以使nose遍历test目录:

1、使用--exe参数,如nosetests -v --exe;

2、对于所有的test目录和文件执行chmod -x。

转载于:https://www.cnblogs.com/iwalkfreely/p/4204570.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值