一、断言方法

方法

检查 版本
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 3.1
assertIsNot(a,b) a is not b 3.1
assertIsNone(x) x is None 3.1
assertIsNotNone(x) x is not None 3.1
assertIn(a,b) a in b 3.1
assertNotIn(a,b) a not in b 3.1
assertIsInstance(a,b) isinstance(a,b) 3.2
assertNotIsInstance(a,b) not isinstance(a,b) 3.2
 import unittest

 class TestAssert(unittest.TestCase):

     def test_equal(self):
self.assertEqual(2+2, 4)
self.assertEqual("python", "python")
self.assertNotEqual("hello", "python") def test_in(self):
self.assertIn("hello", "hello world")
self.assertNotIn("hi", "hello") def test_true(self):
self.assertTrue(True)
self.assertFalse(False) if __name__ == '__main__':
unittest.main()

二、测试用例的组织与discover()方法

  要执行多个不同文件下的测试用例,可以使用unittest中的TestLoader类提供的discover()方法查找要用到的测试用例。该类根据各种标准加载测试用例,并将它们返回给测试套件。正常情况下,不需要创建这个类的实例。unittest提供了可以共享的defaultTestLoader类,可以使用其子类或方法创建实例,discover()方法就是其中之一。

  discover(start_dir, pattern='test*.py', top_level_dir=None)

  找到指定目录及其子目录下的所有测试模块,只有匹配的文件名才能被加载。如果启动的不是顶层目录,那么顶层目录必须单独指定。

  • start_dir:待测试的模块名或测试用例目录。
  • pattern='test*.py':测试用例文件名的匹配原则。
  • top_level_dir=None:测试模块的顶层目录,如果没有顶层目录,则默认为None

  一个单元测试的文件目录如下:

两个不相关的判断闰年功能(leap_year.py)和计算器功能(calculator.py)的代码文件分别对应两个测试文件,calculator.py:

 # 计算器类
class Calculator:
def __init__(self, a, b):
self.a = int(a)
self.b = int(b) def add(self):
return self.a + self.b def sub(self):
return self.a - self.b def mul(self):
return self.a * self.b def div(self):
return self.a / self.b

test_calculator.py文件:

 import unittest

 class TestAssert(unittest.TestCase):

     def test_equal(self):
self.assertEqual(2+2, 4)
self.assertEqual("python", "python")
self.assertNotEqual("hello", "python") def test_in(self):
self.assertIn("hello", "hello world")
self.assertNotIn("hi", "hello") def test_true(self):
self.assertTrue(True)
self.assertFalse(False) if __name__ == '__main__':
unittest.main()

leap_year.py文件:

 class LeapYear:

     def __init__(self, year):
self.year = int(year) def answer(self):
year = self.year
if year % 100 == 0:
if year % 400 == 0:
# 整百年能被400整除的是闰年
return "{0}是闰年".format(year)
else:
return "{0}不是闰年".format(year)
else:
if year % 4 == 0:
# 非整百年能被4整除的是闰年
return "{0}是闰年".format(year)
else:
return "{0}不是闰年".format(year)

test_leap_year.py文件:

 import unittest

 from files.unit_test.leap_year import LeapYear

 class TestLeapYear(unittest.TestCase):
def test_2000(self):
ly = LeapYear(2000)
self.assertEqual(ly.answer(), "2000是闰年") def test_2004(self):
ly = LeapYear(2004)
self.assertEqual(ly.answer(), "2004是闰年") def test_2017(self):
ly = LeapYear(2017)
self.assertEqual(ly.answer(), "2017不是闰年") def test_2100(self):
ly = LeapYear(2100)
self.assertEqual(ly.answer(), "2100不是闰年") if __name__ == '__main__':
unittest.main()

最后,用一个执行文件对这两个测试文件进行执行run_tests.py文件:

 import unittest

 # 定义测试用例的目录为当前目录中的unit_test
test_dir = './'
suits = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suits)

discover()方法会自动根据测试用例目录(unit_test下)查找测试用例文件(test*.py),并将找到的测试用例添加到测试套件中,因此,可以直接通过run()方法执行测试套件suits.这种方式极大地简化了测试用例的查找,我们需要做的是按照文件的匹配规则创建测试文件即可。

Selenium实战(四)——unittest单元测试2(断言方法+discover()多测试用例的执行)的更多相关文章

  1. unittest 单元测试框架断言方法

    unittest单元测试框架的TestCase类下,测试结果断言方法:Assertion methods 方法 检查 版本 assertEqual(a, b)  a == b assertNotEqu ...

  2. unittest常用的断言方法

    unittest常用的断言方法 #msg:判断不成立时需要反馈的字符串 assertEqual(self, first, second, msg=None) --判断两个参数相等:first == s ...

  3. Selenium(十七):unittest单元测试框架(三) 脚本分析、编写Web用例

    1. 带unittest的脚本分析 也许你现在心里还有疑问,unittest框架与我们前面所编写的Web自动化测试之间有什么必然联系吗?当然有,既然unittest可以组织.运行测试用例,那么为什么不 ...

  4. python的unittest单元测试框架断言整理汇总

    自动化脚本最重要的是断言,正确设置断言以后才能帮助我们判断测试用例执行结果. 一.先说说unittest常用的断言吧 常用的就以下几个,网上一搜一大堆.python版本2.7以上都可以调用了. 断言语 ...

  5. unittest中的断言方法

    方法        用途 assertEqual(a,b)      a=b assertNotEqual(a,b)    a!=b assertTrue(x)     x为True assertFa ...

  6. unittest 常用的断言方法

    1.assertEqual(self, first, second, msg=None) --判断两个参数相等:first == second 2.assertNotEqual(self, first ...

  7. Selenium 2自动化测试实战26(unittest单元测试框架)

    一.unittest单元测试框架 1.认识单元测试 1.断言方法 #计算器类 #coding:utf-8 #计算器类 class Count: def __init__(self,a,b): self ...

  8. 测试教程网.unittest教程.7. 各种断言方法

    From: http://www.testclass.net/pyunit/assert/ 背景 unittest支持各种断言方法. 断言列表 官方文档 方法 检查点 assertEqual(a, b ...

  9. unittest单元测试框架

    unittest单元测试框架 概述: 单元测试框架主要用来完成以下三件事: 提供用例组织与执行:当测试用例只有几条时,可以不必考虑用例的组织,但是当用例达到成百上千条时,大量的用例堆砌在一起,就产生了 ...

随机推荐

  1. 四 Shell条件测试

    条件测试操作 在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作,有时也会通过与if等条件语句相结合,让我们可以方便的完成判断. 语法格式 test 选项 文件名或目录名 ...

  2. ORB-SLAM2 初体验 —— 配置安装

    转载请注明出处,谢谢 原创作者:MingruiYU 原创链接:https://www.cnblogs.com/MingruiYu/p/12286752.html ORB-SLAM2作为目前应用最广泛的 ...

  3. Codeforces_731_B

    http://codeforces.com/problemset/problem/731/B 模拟模拟. #include<iostream> #include<cstring> ...

  4. POJ_1564_dfs

    题目描述: 每组数据给定一个大的数,和一系列降序的数值,要求列出不重复的数值中挑选的数的和为大数的方案,每一种方案里,每个数值最多只能使用一次. 思路: dfs基础题,每次记录大数和当前总和的差值,当 ...

  5. HDU_3853_区间dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3853 dp[i][j]表示由空白串刷成b的从i到j位所需要的最小次数. 然后在比较a和b的每一位,再次更新dp表 ...

  6. 9.3.1 map端连接- DistributedCache分布式缓存小数据集

    1.1.1         map端连接- DistributedCache分布式缓存小数据集 当一个数据集非常小时,可以将小数据集发送到每个节点,节点缓存到内存中,这个数据集称为边数据.用map函数 ...

  7. centos6.5下oracle11g下OGG单向复制

    命名规范: local==> l remote==> r extract==> x data pump==> p ------------------------------- ...

  8. Windows+Python+Selenium基础篇之1-环境搭建

    1.所需工具包1.1Selenium for python1.2  Python  1.3  Notepad++或python IDE 2.  环境搭建2.1  下载和安装Pythonpython2. ...

  9. [RHEL8]开启BBR

    # sysctl net.ipv4.tcp_congestion_control net.ipv4.tcp_congestion_control = cubic # sysctl net.ipv4.t ...

  10. 珠峰-webpack1

    #### sourcemap #### watch 选项 #### 3个常用的小插件. #### 前端webpack的自己的mock #### 服务端引用了webpack的插件. #### resol ...