详解python单元测试框架unittest

yipeiwu_com6年前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导入。

相关文章

python黑魔法之编码转换

我们在使用其他语言的库做编码转换时,对于无法理解的字符,通常的处理也只有两种(或三种): 抛异常 替换成替代字符 跳过 但是在复杂的现实世界中,由于各种不靠谱,我们...

基于Python实现的微信好友数据分析

基于Python实现的微信好友数据分析

最近微信迎来了一次重要的更新,允许用户对”发现”页面进行定制。不知道从什么时候开始,微信朋友圈变得越来越复杂,当越来越多的人选择”仅展示最近三天的朋友圈”,大概连微信官方都是一脸的无可奈...

Python-ElasticSearch搜索查询的讲解

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有...

使用python 写一个静态服务(实战)

使用python 写一个静态服务(实战)

师父布置的任务,让我写一个服务练练手,搞清楚socket的原理和过程后跑了一个小demo,很有成就感,代码内容也比较清晰易懂,很有教育启发意义。 代码 # coding:utf-8...

windows下Python安装、使用教程和Notepad++的使用教程

windows下Python安装、使用教程和Notepad++的使用教程

一、Python下载 1.进入Python官网:https://www.python.org/ 2.选择windows版本(Download > Windows) 3.点击下载P...