详解python单元测试框架unittest

yipeiwu_com5年前Python基础

一:unittest是python自带的一个单元测试框架,类似于java的junit,基本结构是类似的。

基本用法如下:

1.用import unittest导入unittest模块

2.定义一个继承自unittest.TestCase的测试用例类,如

class abcd(unittest.TestCase):

3.定义setUp和tearDown,这两个方法与junit相同,即如果定义了则会在每个测试case执行前先执行setUp方法,执行完毕后执行tearDown方法。

4.定义测试用例,名字以test开头,unittest会自动将test开头的方法放入测试用例集中。

5.一个测试用例应该只测试一个方面,测试目的和测试内容应很明确。主要是调用assertEqual、assertRaises等断言方法判断程序执行结果和预期值是否相符。

6.调用unittest.main()启动测试

7.如果测试未通过,则会显示e,并给出具体的错误(此处为程序问题导致)。如果测试失败则显示为f,测试通过为.,如有多个testcase,则结果依次显示。

一个单testcase的简单的例子:

# -*- coding:UTF-8 -*-
'''
Created on 2015年3月24日

@author: Administrator
'''
import unittest
from selenium import webdriver
import time


class TestCase1(unittest.TestCase):


  def setUp(self):
    self.driver=webdriver.Firefox()
    self.base_url="http://www.baidu.com"


  def tearDown(self):
    self.driver.quit()


  def testCase1(self):
    driver=self.driver
    driver.get(self.base_url)
    print "将窗口最大化"
    driver.maximize_window()
    time.sleep(10)


if __name__ == "__main__":
  unittest.main()

一个多testcase的例子:

# -*- coding:UTF-8 -*-
'''
Created on 
@author: Administrator
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException,\
  NoAlertPresentException
import HTMLTestRunner
#form selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Baidu(unittest.TestCase):
  def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://www.baidu.com/?tn=98012088_4_dg&ch=3"
    self.verificationErrors = []
    self.accept_next_alert = True
    self.driver.get(self.base_url)

  def test_baidu_search(self):
    '''百度搜索'''
    driver = self.driver
#    driver.get(self.base_url + "/")
    try:
      driver.find_element_by_id("kw").send_keys("selenium webdriver")
      driver.find_element_by_id("su").click()
    except:
      driver.get_screenshot_as_file('D:\\workspace\\python_prictise\\src\\error.png')
    time.sleep(2)
    driver.close()

  def test_baidu_set(self):
    '''百度新闻'''
    driver = self.driver
    driver.find_element_by_name("tj_trnews").click()
    self.assertEqual(driver.title,u'百度新闻搜索——全球最大的中文新闻平台',"switch to baidu news faile!")
#    time.sleep(2)

  def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException: return False
    return True

  def is_alert_present(self):
    try: self.driver.switch_to_alert()
    except NoAlertPresentException: return False
    return True

  def close_alert_and_get_its_text(self):
    try:
      alert = self.driver.switch_to_alert()
      alert_text = alert.text
      if self.accept_next_alert:
        alert.accept()
      else:
        alert.dismiss()
      return alert_text
    finally: self.accept_next_alert = True

  def tearDown(self):
    self.driver.quit()
    self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":  
  unittest.main()

二:跳过单个testcase和testclass的方法

在unittest中也支持类似junit中的跳过单个测试case或者测试class的方法,如下:

@unittest.skip(reason)

无条件的跳过被修饰的testcase或者testclass,reason描述为何跳过该测试,为一个字符串;

@unittest.skipIf(condition,reason)

如果条件condition为真,则跳过该testcase或者testclass;

@unittest.skipUnless(condition,reason)

除非条件condition为真,否则跳过被修饰的testcase或者testclass;

@unittest.expectedFailure

标记测试为一个预期失败的测试,但不会作为失败测试统计在结果中;

三:断言

在unittest中用断言来判断是pass还是fail,常见的断言方法如下:

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
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)
assertAlmostEqual(a, b) round(a-b, 7) == 0
assertNotAlmostEqual(a, b) round(a-b, 7) != 0
assertGreater(a, b) a > b 2.7
assertGreaterEqual(a, b) a >= b 2.7
assertLess(a, b) a < b 2.7
assertLessEqual(a, b) a <= b 2.7
assertRegexpMatches(s, re) regex.search(s) 2.7
assertNotRegexpMatches(s, re) not regex.search(s) 2.7
assertItemsEqual(a, b) sorted(a) == sorted(b) and works with unhashable objs 2.7
assertDictContainsSubset(a, b) all the key/value pairs in a exist in b 2.7
assertMultiLineEqual(a, b) strings 2.7
assertSequenceEqual(a, b) sequences 2.7
assertListEqual(a, b) lists 2.7
assertTupleEqual(a, b) tuples 2.7
assertSetEqual(a, b) sets or frozensets 2.7
assertDictEqual(a, b) dicts 2.7
assertMultiLineEqual(a, b) strings 2.7
assertSequenceEqual(a, b) sequences 2.7
assertListEqual(a, b) lists 2.7
assertTupleEqual(a, b) tuples 2.7
assertSetEqual(a, b) sets or frozensets 2.7
assertDictEqual(a, b) dicts 2.7

其他断言方法请查阅官方文档

四:组成测试套件

1.添加数量较少的测试case,可以用如下方法:

suite=unittest.Testsuite()
suite.addTest(testclass(testcase))

这里testclass为测试类的名称,testcase为该测试类下的测试case的名称,为字符串。

2.对于有多个测试类的情况,可以用如下方法:

def createsuite():
  testunit=unittest.TestSuite()
  discover=unittest.defaultTestLoader.discover(testdir,pattern='test_*.py', top_level_dir=None)
  print discover
  for test_suite in discover:
    for testsuit in test_suite:
      testunit.addTest(testsuit)
  return testunit
alltestnames = createsuite()

如此便可以将一个目录下多个测试文件中的testcase导入。

相关文章

Python3正则匹配re.split,re.finditer及re.findall函数用法详解

本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下: re.split re.finditer r...

python3使用print打印带颜色的字符串代码实例

一、实现过程 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关 转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示是27,用八进制...

python+django快速实现文件上传

python+django快速实现文件上传

对于web开来说,用户登陆、注册、文件上传等是最基础的功能,针对不同的web框架,相关的文章非常多,但搜索之后发现大多都不具有完整性,对于想学习web开发的新手来说就没办法一步一步的操作...

Python获取暗黑破坏神3战网前1000命位玩家的英雄技能统计

Python获取暗黑破坏神3战网前1000命位玩家的英雄技能统计

说实在的个人对游戏并没有多大的兴趣,但唯独对暴雪的Diablo系列很有感情,去年年初开始玩Diablo3,断断续续,感觉最麻烦的是选择技能,每次版本更新可能都有更优的build,这对于我...

python 函数内部修改外部变量的方法

如果内部修改外部变量需要nonlocal,global def f1(): print("in f1..") num=111 def f2(): nonlocal num...