幽灵资源网 Design By www.bzswh.com

本文介绍了Python nose单元测试框架的安装与使用 ,分享给大家,具体如下:

安装(Python2下安装)

pip install nose

原理与命名规则

Nose会自动查找源文件、目录或者包中的测试用例,符合正则表达式("htmlcode">

nosetests -h

执行并捕获输出:

nosetests -s

查看nose的运行信息和调试信息:

nosetests -v9

输出xml结果报告:

nosetests --with-xunit

支持测试方法传参:

1)安装:需要下载插件“nose_ittr”:

pip install nose_ittr

2)脚本中使用示例:

# -*- coding: utf-8 -*-
import os
from nose.tools import nottest,istest
from nose_ittr import IttrMultiplier, ittr
curr_dir = os.path.dirname(os.path.abspath(__file__))
class TestCheckChannels(object):
  __metaclass__ = IttrMultiplier
  '''
    测试方法传入两个参数
    参数一:channels_txt_name
    参数二:check_list_txt_name
    使用方法:通过“self.参数名”进行调用
  '''
  @istest
  @ittr(channels_txt_name=["channels.txt"],check_list_txt_name=["check_list.txt"]) 
  def test_check_channels(self):
    channels_txt_path = os.path.join(curr_dir,self.channels_txt_name)
    check_list_txt_path = os.path.join(curr_dir,self.check_list_txt_name)
    the_channels = []
    with open(channels_txt_path) as channels:
      for line in channels.readlines():
        line = line.strip()
        if line != '':
          the_channels.append(line)
    with open(check_list_txt_path) as check_list:
      check_items = check_list.readlines()
      for check_item in check_items:
        if check_item.strip() in the_channels:
          pass
        elif check_item=='\n':
          pass
        else:
          print check_item

3)执行示例:

nosetests --with-html-output --html-out-file=result1.html -v --with-setup-ittr

以上执行将输出html结果报告,但是需要先安装插件:

1)安装:

需要下载插件,在解压缩后在命令行中cd到该目录下:

python setup.py install

通过命令行安装:

pip install nosehtmloutput-2
pip install nose-html-reporting

2)在待测路径打开cmd使用命令如下,就可以执行测试并生成测试结果html文件了:

nosetests --with-html-output --html-out-file=result1.html

工具nose.tools的使用:

1)测试脚本中引入:from nose.tools import nottest,istest;

2)不测试的方法:方法名上加修饰器@nottest;

3)指定为测试方法:方法名上加修饰器@istest(方法名无需符合命名规则);

4)查看要执行的用例列表:nosetests --collect-only -v。

测试项目

 详解Python nose单元测试框架的安装与使用

脚本示例

from nose.tools import nottest,istest
from nose.tools import assert_equal

class TestClass:
  def test_one(self):
    x = "this"
    assert 'h' in x
  def test_two(self):
    x = "hello"
    assert hasattr(x, 'check')
  @nottest
  def test_three(self):
    assert True
  @istest
  def xxxxx(self):
    assert True

class test_haha():
  def setUp(self):
    print("============test class setup==============")
  def teardown(self):
    print("============test class teardown==============")
  def test_xxx(self):
    print "test_xxx"
    assert_equal(9, 9)
  def test_kkk(self):
    print "test_kkk"
    assert_equal(1, 1)

测试执行

详解Python nose单元测试框架的安装与使用 

测试结果

详解Python nose单元测试框架的安装与使用 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Python,nose单元测试,nose,单元测试,python,nose框架

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。