python自动化框架nose

python除了unittest,还有一款更快捷的nose,nose可以说是对unittest的一种简化吧

但是他不需要unittest那种必须有固有的格式,他只需要文件,类名,方法名等含有test就可以

unittest是需要手动来写discover函数来遍历用例的

  • Name my test modules/files starting with ‘test_’.
  • Name my test functions starting with ‘test_’.
  • Name my test classes starting with ‘Test’.
  • Name my test methods starting with ‘test_’.
  • Make sure all packages with test code have an ‘init.py’ file.

官网地址http://pythontesting.net/framework/nose/nose-introduction/

里面介绍的很详细,一下总结几个常用的点:

安装:

easy_install nose 

运行:

nosetests [文件]

nosetests [目录]

如果不加目录,默认执行当前目录下的所有符合nose条件的用例,

如果加目录,则运行指定目录里面符合nose条件的用例

常用参数

-v 把运行中的具体过程输出nosetests -v

simple_example.test_um_nose.test_numbers_3_4 ... ok
simple_example.test_um_nose.test_strings_a_3 ... ok
 
----------------------------------------------------------------------
Ran 2 tests in 0.000s
 
OK

-s 把测试用例中的print输出打印到控制台,这个调试的时候还是挺方便的

复制代码
C:\Users\Administrator>nosetests -s "d:\\00"

thisisprint1
.thisisprint2

------------------------------------------------
Ran 2 tests in 0.044s

OK
复制代码

nose的执行规则:

和unittest一样,会优先执行setUp,最后执行tearDown,和函数的位置没关系

 

复制代码
def tearDown():
        print "teardown"
def test1():
        print 1
        #assert(1==2)
def test2():
        print 2
def setUp():
        print "this setup"

C:\Users\Administrator>nosetests -s -v  "d:\\00"
this setup
testnose.test1 ... 1
ok
testnose.test2 ... 2
ok
teardown
=
------------------------------------------------
Ran 2tests in 0.010s

OK
复制代码

nose里面常用的工具有很多函数,比如类似unittest的assertEqual

使用方法

from nose import tools

然后看下里面有这么多的方法

assert_almost_equal
assert_almost_equals
assert_dict_contains_subset
assert_dict_equal
assert_equal
assert_equals
assert_false
assert_greater
assert_greater_equal
assert_in
assert_is
assert_is_instance
assert_is_none
assert_is_not
assert_is_not_none
assert_items_equal
assert_less
assert_less_equal
assert_list_equal
assert_multi_line_equal
assert_not_almost_equal
assert_not_almost_equals
assert_not_equal
assert_not_equals
assert_not_in
assert_not_is_instance
assert_not_regexp_matches
assert_raises
assert_raises_regexp
assert_regexp_matches
assert_sequence_equal
assert_set_equal
assert_true
assert_tuple_equal
eq_
istest
make_decorator
nontrivial
nontrivial_all
nottest
ok_
raises
set_trace
timed
trivial
trivial_all
with_setup

 

posted @   米 立  阅读(6671)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
点击右上角即可分享
微信分享提示