一、断言方法

断言是对自动化测试异常情况的判断。

# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest
import os,sys,time
import HTMLTestReport #登录
driver =webdriver.Firefox() current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
current_time1 = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print(current_time )
print(current_time1 )
# 必须打印图片路径HTMLTestRunner才能捕获并且生成路径,\image\**\\**.png 是获取路径的条件,必须这样的目录
#设置存储图片路径,测试结果图片可以按照每天进行区分 pic_path = '.\\result\\image\\' + current_time1+'\\' + current_time +'.png'
print(pic_path)
time.sleep(5) #通过if进行断言判断
driver.get("https://baidu.com/")
print(driver.title)
driver.save_screenshot(pic_path)
if u'百度一下,你就知道' == driver.title:
print ('Assertion test pass.')
else:
print ('Assertion test fail.') #通过try抛出异常进行断言判断
driver.get("https://baidu.com/")
driver.save_screenshot(pic_path)
try:
assert u'百度一下,你就知道' == driver.title
print ('Assertion test pass.')
except Exception as e:
print ('Assertion test fail.', format(e)) time.sleep(5)
driver.quit()

方法一,是利用python中Assert方法,采用包含判断,方法二是通过if方法,采用完全相等方法,建议选择第一种方法

这u代表unicode的意思,由于我们这里采用了python 2, 如果你使用pyn3 就不需要,在Python3中,字符串默认采用unicode存储。

二、断言方法

在执行用例的过程中,最终用例是否通过,是通过判断测试的到的实际结果与预测结果是否相等绝顶的。unittest框架的TestCase类提供下面这些方法用于测试结果的判断。

 from calculator import Count
import unittest class TestCount (unittest.TestCase):
def setUp(self):
print("test start") def test_add(self):
j = Count (2, 4)
self.assertEqual (j.add (), 6) def test_add2(self):
i = Count (2,9)
self.assertEqual (i.add (), 11) def tearDown(self):
print("test end") if __name__ == '__main__':
# unittest.main ()
suite = unittest.TestSuite()
suite.addTest(TestCount('test_add2'))
suite.addTest (TestCount ('test_add'))
runner = unittest.TextTestRunner()
runner.run(suite)
方法 检查 版本
assertEqual(a,b) a==b  
assertNotEqual(a,b) a!=b  
asserTrue(x) bool(x) is true  
asserFalse(x) bool(x) is False  
assertIs(a,b) a is b  
assertIsNot(a,b) a is not b  
assertIsNone(x) x is None  
assertIsNoneNot(x) x is not None  
assertIn(a,b) a in b  
assertInNot a not in b  
assertIsInstance(a,b) isinstance(a,b)  
assertNotIsInstance(a,b) not isinstance (a,b)  

***assertEqual(first ,second ,msg =None)

断言第一个参数和第二个参数是否相等,如果 不相等则测试失败。msg为可选参数,用于定义测试失败时打印的信息。

 import unittest

 class assertEqual1(unittest.TestCase):
def setUp(self):
number = input ("Enter a number:")
self.number = int (number) def test_case(self):
self.assertEqual (self.number, 10, msg='Your input is not 10!') def tearDown(self):
pass if __name__ == '__main__':
unittest.main ()

三、assertTrue与assertFalse

用于测试表达式是true还是false。

下面来实现判断一个数是否未数的功能,所谓的质数(又叫素数)是指只能被1和她本身整除的数。

 def is_prime(n):
if n <= 1:
return False
for i in range (2, n):
if n % i == 0:
return False
else:
return True
 from calculator import is_prime
import unittest class test (unittest.TestCase):
def setUp(self):
print("测试开始") def test_case(self):
self.assertTrue (is_prime(3), msg="Is not prime!") def tearDown(self):
print("测试结束") if __name__ == '__main__':
unittest.main ()

在调用is_prime()函数时分别传不同的值来执行测试用例,然后通过assertTrue()断言得到的结果进行判断。

四、assertIn(first,second,msg = None)与assertNotIn(first,second,msg = None)

注:断言第一个参数是否在第二个参数中,第二个表示参数是否包含第一个参数。

 import unittest

 class test (unittest.TestCase):
def setUp(self):
print("测试开始") def test_case(self):
a = 'hello'
b = 'hello word!'
# self.assertIn (b, a, msg='a is not in b')
self.assertNotIn (a, b, msg='a is not in b') def tearDown(self):
print("测试结束") if __name__ == '__main__':
unittest.main ()
  1. assertIs(first,second,msg=None) 与assertIsNot(first,second,msg=None)

断言第一个参数和第二个参数是否为同一个对象。

2.assertIsNone(expr,msg = None)与assertIsNone(expr,msg = None)

断言表达式是否为None对象。

3.assertIsInstance(obj,cls,msg = None)与assertIsInstance(obj,cls,msg = None)

断言obj是否为cls的一个实例。

python+selenium之断言Assertion的更多相关文章

  1. Python+Selenium之断言对应的元素是否获取以及基础知识回顾

    # coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window () ...

  2. python selenium测试用例断言

    1.if ...else ...判断进行断言 #coding=utf-8 from time import * from selenium import webdriver "): driv ...

  3. Python中不尽如人意的断言Assertion

    Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛出异常. >>> assert 1 + 1 ...

  4. 【转】Python中不尽如人意的断言Assertion

    原文地址:Python中不尽如人意的断言Assertion Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛 ...

  5. selenium关于断言的使用

    基本介绍: Selenium工具专门为WEB应用程序编写的一个验收测试工具. Selenium的核心:browser bot,是用JAVASCRIPT编写的. Selenium工具有4种:Seleni ...

  6. Selenium3 + Python3自动化测试系列五——常用断言Assertion

    断言Assertion 验证应用程序的状态是否同所期望的一致. 常见的断言包括:验证页面内容,如标题是否为X或当前位置是否正确,或是验证该复选框是否被勾选. selenium 提供了三种模式的断言:a ...

  7. 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...

  8. 使用python selenium进行自动化functional test

    Why Automation Testing 现在似乎大家都一致认同一个项目应该有足够多的测试来保证功能的正常运作,而且这些此处的‘测试’特指自动化测试:并且大多数人会认为如果还有哪个项目依然采用人工 ...

  9. python+selenium 自动化测试实战

    一.前言: 之前的文章说过, 要写一篇自动化实战的文章, 这段时间比较忙再加回家过11一直没有更新博客,今天整理一下实战项目的代码共大家学习.(注:项目是针对我们公司内部系统的测试,只能内部网络访问, ...

随机推荐

  1. centos6.5下PF_RING安装方法

    参考的是这个大牛的文章:http://blog.csdn.net/fan_hai_ping/article/details/6705170 系统环境:centos6.5 开发版 PF_RING版本:P ...

  2. js学习笔记3:with语句的使用

    with语句 with是ECMAScript规定的内容,主要用于设置代码在特定对象中的作用域. var sMessage = "hello"; with(sMessage) { c ...

  3. iOS内购流程一(协议、税务和银行业务)

    协议.税务和银行业务,这一选项是当你App使用了In-app purchaes时候,你跟苹果签订协议的,需要签订合同和填写你的银行收款等信息 一.填写法人信息 1.登录iTunes Store,点击协 ...

  4. unite2017《Unity企业级支持案例与分析》

    在今天举办的Unite2017开发者大会上,Unity大中华区技术支持总监张黎明以"Unity企业级支持案例与分析"为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加 ...

  5. django 模版 语法与使用

    目录 django 模版语法与使用 django模板语言介绍 (摘自官方文档) 链接 什么是模板? 模板语句的 注释 变量 {{ 变量 }} 点(.)在模板语言中有特殊的含义,用来获取对象的相应属性值 ...

  6. 剑指Offer的学习笔记(C#篇)-- 从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 一 . 个人想法 这个题目搞了一段时间,因为解法好多,比如:是用递归法呢还是循环呢,要不要使用栈呢等等.. 所以,每一种想法 ...

  7. Centos 自动更新git

    首先,要先配置好自己的Git,然后在某一处进行脚本的编写. 比如项目目录为:/home/project,那参考如下来进行 vim /home/project/automatic_git.sh #/bi ...

  8. Python-11-循环

    x = 1 while x <= 100:     print(x)     x += 1   基本上, 可迭代对象是可使用for循环进行遍历的对象. numbers = [0, 1, 2, 3 ...

  9. Charles使用小结

    charles,抓包神器,记录几个测试过程中常用的功能 连接同一局域网的开发机     域名跳转MapRemoteSetting     抓取Https接口 1.下载3.10以上破解版,按如下步骤安装 ...

  10. CodeForces - 507B - Amr and Pins(计算几何)

    Amr loves Geometry. One day he came up with a very interesting problem. Amr has a circle of radius r ...