详解python单元测试框架unittest

作者:df0128 时间:2022-05-31 23:16:58 

一: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导入。

来源:https://blog.csdn.net/df0128/article/details/45770077

标签:python,unittest
0
投稿

猜你喜欢

  • 实例讲解Access数据库在线压缩的实现方法

    2008-11-28 14:29:00
  • Python 内置变量和函数的查看及说明介绍

    2021-06-11 16:12:14
  • set rs=server.CreateObject("adodb.recordset") 的中文详细说明

    2011-03-06 11:21:00
  • 解析python调用函数加括号和不加括号的区别

    2023-11-30 17:29:50
  • JS实现合并json对象的方法

    2023-08-29 15:09:30
  • 把你的数据库置于版本控制之下

    2009-04-25 10:01:00
  • 用Python的Django框架来制作一个RSS阅读器

    2023-01-31 19:17:33
  • ASP生成XML文件

    2009-06-29 16:28:00
  • 基于python实现学生管理系统

    2021-11-24 21:07:48
  • 详解python使用turtle库来画一朵花

    2021-08-30 14:58:49
  • Python浅析生成器generator的使用

    2023-09-02 16:58:40
  • python实现将多个文件分配到多个文件夹的方法

    2023-04-13 20:48:46
  • PHP中curl_setopt函数用法实例分析

    2023-11-22 22:07:22
  • 详解python中@classmethod和@staticmethod方法

    2022-10-24 11:47:16
  • Python random库使用方法及异常处理方案

    2023-10-07 13:43:46
  • JavaScript点击按钮后弹出透明浮动层的方法

    2023-08-05 22:33:29
  • Java动态-代理实现AOP

    2023-07-15 09:33:43
  • python环形单链表的约瑟夫问题详解

    2023-03-02 04:13:10
  • Python实现简易五子棋游戏

    2023-02-04 07:02:22
  • PHP获取当前相对于域名目录的方法

    2023-08-19 18:47:31
  • asp之家 网络编程 m.aspxhome.com