使用Nose运行目录中的所有测试

26 投票
2 回答
22260 浏览
提问于 2025-04-16 19:29

我想在Linux的命令行中输入一行命令,就能运行当前目录下的所有测试。在某些目录下,这个方法很好用。但在其他目录下,当我输入“nosetests”时,测试却没有运行。如果我单独调用每个测试,它们是可以运行的,但我希望它们能自动全部运行。以下是一个无法正常工作的目录:

/extwebserver
    __init__.py
    test_Detection.py
    test_Filesystem.py 
    test_Hardware.py
    ...

当我在父目录下运行“nosetests”时,某个子目录中的所有测试都能运行,但/extwebserver或其他子目录以及父目录本身的测试都没有运行。

编辑 这是输出结果:

matthew@Matthew-Laptop:~/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing$ nosetests -vv --collect-only
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/baseTestCase.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/run.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Detection.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Filesystem.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Hardware.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Mode.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_System.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_View.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/extwebserver/test_Webserver.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/mocks/bottle.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/utils/test_timestamps.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testCamera.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testCameraManager.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testMainControllerServer.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testMode.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testPASEServerClient.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testView.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/testViewsManager.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/test_commission/test_FSM.py is executable; skipped
nose.selector: INFO: /home/matthew/Documents/ParkAssist/m3/linux/appfs/master/usr/bin/piopio/testing/test_commission/test_Webserver.py is executable; skipped
test_add (testing.utils.test_config.test_config) ... ok
test_add_set (testing.utils.test_config.test_config) ... ok
test_load (testing.utils.test_config.test_config) ... ok
test_load_valid (testing.utils.test_config.test_config) ... ok
test_modify_set (testing.utils.test_config.test_config) ... ok
test_save (testing.utils.test_config.test_config) ... ok
test_update (testing.utils.test_config.test_config) ... ok
test_get_full_version (testing.utils.test_controller.test_controller) ... ok
test_get_hwaddr (testing.utils.test_controller.test_controller) ... ok
test_get_ip (testing.utils.test_controller.test_controller) ... ok
test_get_netmask (testing.utils.test_controller.test_controller) ... ok
test_get_version (testing.utils.test_controller.test_controller) ... ok
test_FloatEncoder (testing.utils.test_fileio.test_fileio) ... ok
test_read_file (testing.utils.test_fileio.test_fileio) ... ok
test_read_json_string (testing.utils.test_fileio.test_fileio) ... ok
test_system_to_text (testing.utils.test_fileio.test_fileio) ... ok
test_write_file (testing.utils.test_fileio.test_fileio) ... ok
test_get_hwaddr (testing.utils.test_netdev.test_netdev) ... ok
test_get_ip (testing.utils.test_netdev.test_netdev) ... ok
test_get_netmask (testing.utils.test_netdev.test_netdev) ... ok
test_set_datetime (testing.utils.test_syncdatetime.test_syncdatetime) ... ok
test_cast (testing.utils.test_tools.test_tools) ... ok
test_get_image (testing.testdetection.test_CameraGuardian.test_CameraGuardian) ... ok

----------------------------------------------------------------------
Ran 23 tests in 0.003s

2 个回答

15

你可以在命令行中使用 --exe 来强制 nose 把可执行文件当作有效的测试。如果你觉得每次都要写 --exe 很麻烦,可以在你的主目录下创建一个 .noserc(适用于 unix/linux)或 nose.cfg(适用于 windows)文件,然后在里面加上这一行:

exe = True

这样就不用每次都手动输入了。

56

以下内容摘自Python Testing: Beginner's Guide,作者是Daniel Arbuckle:

Nose会在那些以testTest开头的目录和模块中寻找测试,或者在名字中包含'_''.''-'后面跟着testTest的地方。这是默认的查找方式,但实际上并不是全部。

nose单元测试框架的扩展介绍显示,你可以通过运行以下命令来查看nose的测试发现算法的详细输出:

nosetests -vv --collect-only

当我在一个目录上运行上述命令时,我注意到Nose会跳过可执行文件。如果这是你的问题,你需要把文件的模式改为不可执行。在Mac OS X或Linux上,可以用以下命令实现:

chmod 644 file.py

或者类似的命令。

如果你知道你的模块/文件是安全的,可以用--exe选项来运行Nose,这样它会“在可执行的Python模块中寻找测试”(来自`man nosetests):

nosetests --exe

撰写回答