详解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导入。

相关文章

基于Django框架的权限组件rbac实例讲解

基于Django框架的权限组件rbac实例讲解

1.基于rbac的权限管理 RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联。简单地说,一个用户拥有若干角色,一个角色拥...

python3 判断列表是一个空列表的方法

python3 判断空列表 @(python3) 有个判断列表是否为空的需求,试了好多方式,比如: a = [] if a is not None: COMMAND a =...

Python字典操作简明总结

1.dict()创建字典 复制代码 代码如下: >>> fdict = dict((['x', 1], ['y', 2])) >>> fdict {'...

Django框架中处理URLconf中特定的URL的方法

有时你有一个模式来处理在你的URLconf中的一系列URL,但是有时候需要特别处理其中的某个URL。 在这种情况下,要使用将URLconf中把特殊情况放在首位的线性处理方式 。 比方说,...

pycharm远程开发项目的实现步骤

pycharm远程开发项目的实现步骤

你是不是在学习python的时候在使用虚拟机系统进行开发,来回切换很是不方便,那么今天给大家推荐一个pycharm强大的功能。 接下来我们利用这个django项目创建一个子app,测验...