在执行测试用例时,有时候有些用例是不需要执行的,那我们怎么办呢?难道删除这些用例?那下次执行时如果又需要执行这些用例时,又把它补回来?这样操作就太麻烦了。

unittest提供了一些跳过指定用例的方法

  • @unittest.skip(reason):强制跳转。reason是跳转原因
  • @unittest.skipIf(condition, reason):condition为True的时候跳转
  • @unittest.skipUnless(condition, reason):condition为False的时候跳转
  • @unittest.expectedFailure:如果test失败了,这个test不计入失败的case数目
    # coding = utf-8
    import unittest
    import warnings
    from selenium import webdriver
    from time import sleep
    # 驱动文件路径
    driverfile_path = r'D:\coship\Test_Framework\drivers\IEDriverServer.exe' class CmsLoginTest(unittest.TestCase):
    def setUp(self):
    # 这行代码的作用是忽略一些告警打印
    warnings.simplefilter("ignore", ResourceWarning)
    self.driver = webdriver.Ie(executable_path=driverfile_path)
    self.driver.get("http://172.21.13.83:28080/") def tearDown(self):
    self.driver.quit() @unittest.skip("用户名密码都为空用例不执行")
    def test_login1(self):
    '''用户名、密码为空'''
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message1 = self.driver.find_element_by_css_selector("[for='loginName']").text
    error_message2 = self.driver.find_element_by_css_selector("[for='textfield']").text
    self.assertEqual(error_message1, '用户名不能为空')
    self.assertEqual(error_message2, '密码不能为空') @unittest.skipIf(3 > 2, "3大于2,此用例不执行")
    def test_login3(self):
    '''用户名、密码正确'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    sleep(1)
    self.driver.switch_to.frame("topFrame")
    username = self.driver.find_element_by_css_selector("#nav_top>ul>li>a").text
    self.assertEqual(username,"autotest") @unittest.skipUnless(3 < 2,"2没有大于3,此用例不执行")
    def test_login2(self):
    '''用户名正确,密码错误'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector(".errorMessage").text
    self.assertEqual(error_message, '密码错误,请重新输入!') @unittest.expectedFailure
    def test_login4(self):
    '''用户名不存在'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("test007")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector(".errorMessage").text
    self.assertEqual(error_message, '用户名不存在!') def test_login5(self):
    '''用户名为空'''
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector("[for='loginName']").text
    self.assertEqual(error_message, '用户不存在!') def test_login6(self):
    '''密码为空'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector("[for='textfield']").text
    self.assertEqual(error_message, '密码不能为空') if __name__ == "__main__":
    unittest.main(verbosity=2)

unittest详解 跳过用例的执行(skip)的更多相关文章

  1. unittest详解(二) 跳过用例的执行(skip)

    在执行测试用例时,有时候有些用例是不需要执行的,那我们怎么办呢?难道删除这些用例?那下次执行时如果又需要执行这些用例时,又把它补回来?这样操作就太麻烦了. unittest提供了一些跳过指定用例的方法 ...

  2. unittest详解(三) 测试套件(TestSuite)

    在前面一章中示例了如何编写一个简单的测试,但有两个问题: 我们知道测试用例的执行顺序是根据测试用例名称顺序执行的,在不改变用例名称的情况下,我们怎么来控制用例执行的顺序呢? 一个测试文件,我们直接执行 ...

  3. unittest详解(一) unittest初识

    unittest是python内置的一个单元测试框架,在学习怎么使用它之前,我们先来了解它的一些概念和原理. Test Case:测试用例,一个TestCase的实例就是一个测试用例.什么是测试用例呢 ...

  4. Pytest_跳过用例的执行(7)

    pytest跳过用例执行的用法与unittest跳过用例大致相同. pytest跳过用例的方法如下: pytest.mark.skip(reason):无条件用例.reason是跳过原因,下同. py ...

  5. unittest详解(四) 批量执行用例(discover)

    前面我们说了,对于不同文件用例,我们可以通过addTest()把用例加载到一个测试套件(TestSuite)来统一执行,对于少量的文件这样做没问题,但是如果有几十上百个用例文件,这样做就太浪费时间了. ...

  6. Python单元测试框架 unittest详解

    一 整体结构概览 unittest原名为PyUnit,是由java的JUnit衍生而来.对于单元测试,需要设置预先条件,对比预期结果和实际结果. TestCase :通过继承TestCase类,我们可 ...

  7. 四大传值详解:属性传值,单例传值,代理传值,block传值

    一:属性传值 传值情景:从前一个页面向后一个页面传值 a.在后一个页面,根据传值类型和个数,写属性 b.在前一个页面, 为属性赋值 c.在后一个页面, 使用值 例如: 第一个视图: #import & ...

  8. unittest详解(七) 自动生成测试报告

    用例执行完成后,执行结果默认是输出在屏幕上,其实我们可以把结果输出到一个文件中,形成测试报告. unittest自带的测试报告是文本形式的,如下代码: import unittest if __nam ...

  9. unittest详解(六) 断言

    我们在执行测试用例时,怎么来判断这条用例是否通过呢?唯一的办法就是拿实际结果和预期结果进行比较,如果一致用例就是通过的,否则用例就是失败的.在python中这种比较的方法就叫做断言,unittest框 ...

随机推荐

  1. pytorch seq2seq闲聊机器人beam search返回结果

    decoder.py """ 实现解码器 """ import heapq import torch.nn as nn import con ...

  2. 2020年必须掌握的硬核技能k8s

    Kubernetes 是一个软件系统,使你在数以万计的电脑节点上运行软件时就像 所有节点是以单个大节点一样, 它将底层基础设施抽象,这样做同时简化了应用开发.部署,以及对开发和运维团队的管理. Kub ...

  3. thinkphp5 input坑

    取值方式改了而已?a1=1&a2=2这种可以用input(get.) a1/1/a2/2 用input('a1')和input('a2') post方法当然是input('post.') 我觉 ...

  4. php token验证范例

    <?php $module = $_GET['module']; $action = $_GET['action']; $token = md5sum($module.date('Y-m-d', ...

  5. python学习12类

    '''''''''类:具有相同特性和行为的对象抽象为类特性——>属性Property行为——>方法class:关键字'''class Boxes():#类的第一行格式 '''立方体类''' ...

  6. c++中set 的用法

    1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...

  7. JAVA企业级应用TOMCAT实战(一)

    一. Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共 ...

  8. C# 9 新特性:代码生成器、编译时反射

    前言 今天 .NET 官方博客宣布 C# 9 Source Generators 第一个预览版发布,这是一个用户已经喊了快 5 年特性,今天终于发布了. 简介 Source Generators 顾名 ...

  9. 李宏毅机器学习--PM2.5预测

    一.说明 给定训练集train.csv,要求根据前9个小时的空气监测情况预测第10个小时的PM2.5含量. 训练集介绍: (1).CSV文件,包含台湾丰原地区240天的气象观测资料(取每个月前20天的 ...

  10. mysql 之 清空表中数据

    清空表的时候注意外键约束 命令版 查询数据库中所有表名select table_name from information_schema.tables where table_schema='DB_n ...