nose进行python自动化测试
一.简单介绍
1.nose能自动识别并收集在(当前工作目录及其子目录)下源文件, 目录, 包中的测试文件,只要符合以Test或test开头,如test_one.py
2.nose能自动识别继承unittest.TestCase的测试文件, 不需要文件名匹配,如: class one(unittest.TestCase)
3.在linux系统中的测试文件如果是可执行文件,默认情况下是不执行的. 如果要执行可以加上 --exe 如nosetests --exe 或者去掉可执行属性 chmod 644 xxx.py
二.安装nose及插件
1.安装nose: pip install nose
2.查看nose插件: nosetests -p
3.安装nose插件: pip install 插件名字
三. nose脚本命令
nosetests [options] [(optional) test files or directories]
四.nose的配置文件(目前没有测试过)
除了使用命令行这种方式之外,还可以在根目录下放置配置文件,配置文件的类型为.noserc或nose.cfg文件。配置文件都是标准的ini内容格式。例如:
[nosetests]
verbosity=3
with-doctest=1
五. 测试用例
1.查看nose的帮助文档: nosetests -h
2.nose执行单个文件(函数)的test
2.1 nosetests xx.py 执行xx这个Python文件的test
2.2 nosetests xx.py:TestClass.function 执行xx文件中TestClass类下的function
2.3 xx.py不一定都是当前目录下的, 可以是绝对路径的任一目录下
3.执行目录下的所有test
3.1 nosetests 执行当前目录及其子目录下的所有test
3.2 nosetests -w /path/to/tests 执行指定目录及其子目录下的所有test
3.3 注意 更高版本的nose不需要 -w 选项
4.-v 显示详细信息
nosetests -v
例如:
[root@mybuildvm impl]# nosetests -v
test_one (host.services.tools.cx_python_commands.impl.command.test.test_one.One) ... ok
test_new (host.services.tools.cx_python_commands.impl.command.test.test_patient.PatientTestCase) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.419s
OK
5.-s 输出打印信息
nosetests -s
6.更多信息查询
nosetests -h