Nose框架使用

更新时间:2023-10-15 20:47:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Nose框架使用

1、Nosetest常用参数

1) nosetests –v :debug模式,看到具体执行情况,推荐大家执行时用这个选项 2) nose会捕获标准输出,调试的print代码默认不会打印。nosetest –s 可打开

output输出,否则全部通过时不打印stdout。

3) 默认nosetests会执行所有的case,若想单独只执行一个case,执行nosetest

--tests 后跟要测试的文件(nosetests后面直接跟文件名,其实也可以直接运行该case)。

4) nosetest --pdb-failures :失败时,立马调试。这选项很赞,可看到失败时的即时

环境。

5) nosetests --collect-only -v :不运行程序,只是搜集并输出各个case的名称 6) nosetests -x :一旦case失败立即停止,不执行后续case 7) nosetests –failed :只执行上一轮失败的case

该框架可用的函数

以下函数都可以直接使用,无需导入头文件等

Safe封装Assert语句 AssertEqual

AssertEqual(s1, s2, msg=''):

@note:判断s1和s2是否相等,两者类型必须一致 [例子]:\\r\\n

AssertEqual(\鲜花\鲜花\检查前面两个字符串是否相等\

@param: s1,s2待比较的对象,对象类型不定,但是两个对象 必须相同类型 @rtype:int

@rvalue: 相等返回0,不相等抛异常

AssertNotEqual

AssertNotEqual(s1, s2, msg=''):

@note:判断s1和s2是否不相等,两者类型必须一致 [例子]:\\r\\n

AssertNotEqual(\鲜花\鲜花a\如果前面两个字符串相等,则输出该信息\ @param: s1,s2待比较的对象,对象类型不定,但是两个对象必须相同类型 @rtype:int

@rvalue: 相等返回0,不相等返回抛异常

AssertTrue

AssertTrue(s, msg=''): @note:断言s参数为True [例子]:\\r\\n

AssertTrue(1,\如果前面一个参数为假,则输出该信息\ @param: s,待判断是否为True的对象 @rtype:int

@rvalue: s对象是真则返回0,s对象是假,则抛出异常

AssertFalse

AssertFalse(s, msg=''): '''

@note:断言s参数为False [例子]:\\r\\n

AssertFalse(0,\如果前面参数的值为真,则输出该信息\ @param: s,带判断是否为False的对象 @rtype:int

@rvalue: s对象为假则返回0,s对象为真则抛出异常

AssertIn

AssertIn(s, l, msg=''): @note:判断s是否属于l [例子]:\\r\\n

AssertIn(1,[2,3,1,5],

\如果第一个参数不属于第二个参数对应的集合,则输出该信息\ @param:s表示元素,l表示集合,msg表示出错是打印的消息

@rvalue:int

@rtype:如果l中包含s,返回0,如果l中不包含s,则抛出异常

AssertNotIn

AssertNotIn(s, l, msg=''): @note:判断s是否不属于l [例子]:\\r\\n

AssertNotIn(2,[3,4,5,6],

\如果第一个参数属于第二个参数对应的集合,则输出该参数\ @param:s表示元素,l表示集合,msg表示出错是打印的消息 @rvalue:int

@rtype:如果l中不包含s,返回0,如果l中包含s,则抛出异常

AssertInclude

AssertInclude(s, t, msg=''): @note:判断s是否包含t, [例子]:\\r\\n

AssertInclude([1,2],[1,2,3,4],

\如果第二个参数对应的列表不包含第一个参数对应的列表,则输出该信息\ @param:s和t都是列表类型 @rvalue:int

@rtype:如果s中包含t,返回0,如果s中不包含t,则抛出异常

AssertExclude

AssertExclude

@note:判断s是否不包含t [例子]:\\r\\n

AssertExclude([1,2],[3,4,6],

\如果第二个参数对应的列表包含第一个参数对应的列表,则输出该信息\ @param:s和t都是列表类型 @rvalue:int

@rtype:如果s中不包含t,返回0,如果s中包含t,则抛出异常 '''

AssertGreater

AssertGreater(s1, s2, msg=''):

@note:判断s1是否大于s2,两者类型必须一致 [例子]:\\r\\n

AssertEqual(3,2,\检查第一个对象是否大于第二个对象\

@param: s1,s2待比较的对象,对象类型不定,但是两个对象 必须相同类型 @rtype:int

@rvalue: 相等返回0,不相等抛异常

Nose自带assert语句 nose.tools

assert_almost_equal(first, second, places=7, msg=None) assert_almost_equals(first, second, places=7, msg=None) assert_equal(first, second, msg=None) assert_equals(first, second, msg=None) assert_false(expr, msg=None)

assert_not_almost_equal(first, second, places=7, msg=None) assert_not_almost_equals(first, second, places=7, msg=None) assert_not_equal(first, second, msg=None) assert_not_equals(first, second, msg=None) assert_true(expr, msg=None) eq_(a, b, msg=None) ok_(expr, msg=None)

unittest的assert

http://docs.python.org/library/unittest.html

Method

Checks that

assertEqual(a, b) a == b assertNotEqual(a, b) a != b

assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIs(a, b) a is b assertIsNot(a, b) a is not b assertIsNone(x) x is None assertIsNotNone(x)

x is not None

New in 2.7 2.7 2.7 2.7

assertIn(a, b) assertNotIn(a, b) assertIsInstance(a, b) assertNotIsInstance(a, b) assertAlmostEqual(a, b) assertNotAlmostEqual(a, b) assertGreater(a, b)

assertGreaterEqual(a, b) assertLess(a, b)

assertLessEqual(a, b)

assertRegexpMatches(s, re) assertNotRegexpMatches(s, re)

assertItemsEqual(a, b)

a in b a not in b

isinstance(a, b) not isinstance(a, b) round(a-b, 7) == 0 round(a-b, 7) != 0 a > b a >= b a < b a <= b

regex.search(s) not regex.search(s) 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7

assertDictContainsSubset(a, b) assertRaises(exc, fun, *args, fun(*args, **kwds)raises exc

**kwds)

assertRaisesRegexp(exc, re, fun(*args, **kwds)raises exc and the message

2.7

fun, *args, **kwds) matches re

sorted(a) == sorted(b) and works with unhashable 2.7

objs

all the key/value pairs in a exist in b 2.7

utlib封装函数

call_cmd(command, cwd=None, input=None, env=None):

@note: 实际执行 cmd,内部调用subprocess.Popen实现。与os.system仅有一个返回值相比,call_cmd会返回一个(retcode, stdout, stderr)的元组

@input:command 执行的脚本命令,可包含参数,如果参数含有变量可用如下形式

Parmeter=1

Call_cmd(fun='a.sh'+parmeter)

两者在效果上是一致的

call_function(shell, fun, cwd=None, input=None, env=None, setx=True):

@note: 实际上执行 source shell; set -x; call_cmd(fun),针对shell脚本中的函数粒度 @input:shell脚本的名称 command 执行的脚本命

@example: call_function(shell='./define.sh',fun='third_step_hadoop \

本文来源:https://www.bwwdw.com/article/nahf.html

Top