unittest

unittest的discover方法

99封情书 提交于 2019-12-02 22:54:00
转载: https://www.cnblogs.com/imyalost/p/9048386.html discover()方法   discover(start_dir, pattern ='test *.py', top_level_dir = None )   start_dir: 要测试的模块名或测试用例目录;   pattern='test*.py': 表示用例文件名的匹配原则,下面的例子中匹配文件名为以“test”开头的“.py”文件,星号“*”表示任意多个字符;   top_level_dir=None: 测试模块的顶层目录,如果没有顶层目录,默认为None;   该方法通过从指定的开始目录递归到子目录中查找所有测试模块,并返回包含它们的TestSuite对象,只有与模式匹配测试文件和可导入的模块名称才会被加载。   所有测试模块必须可以从项目的顶层导入,如果起始目录不是顶层目录,则顶层目录必须单独指定。   如果一个测试文件的名称符合pattern,将检查该文件是否包含 load_tests() 函数,如果 load_tests() 函数存在,则由该函数负责加载本文件中的测试用例。   如果不存在,就会执行loadTestsFromModule(),查找该文件中派生自TestCase 的类包含的 test 开头的方法。 实例: # coding=utf-8

python-unittest

匿名 (未验证) 提交于 2019-12-02 22:51:30
TestCase是一个测试用例,一个完整的测试用例包括:测试前准备setUp、测试执行代码run、测试后环境还原tearDown。 TestSuite,是多个测试用例的集合,测试套件TestSuite可以嵌套测试套件。 TestLoader加载用例到TestSuite; TextTestRunner执行测试用例,保存测试结果到TextTestResult中。 Test fixture是一个用例环境的搭建和销毁过程。 1、测试用例执行过程: 测试用例Testcase完成后,使用TestSuite的TestLoader方法加载到测试套件, 然后有TextTestRunner运行测试套件,把测试结果保存在TextTestResult中,,每一个测试方法都必须以test开头,否则不会被unittest识别, 在测试执行过程中,我们使用到一个参数:verbosity(0-不输出每一项执行结果、1-输出每一个测试结果、2-输出详细执行结果。) 2、测试断言: 3、uniitest 三种跳过用例执行方法: a、unittest.skip(msg) 直接跳过用例执行,不进行条件判断; b、unittest.skipIf(condtion) 满足条件不执行 c、unittest.skipUnless(condtion) 满足条件才执行 加载测试套件Suite import os import

python unittest之断言及示例

匿名 (未验证) 提交于 2019-12-02 22:51:30
# _*_ coding:utf-8 _*_ import unittest import sys reload(sys) sys.setdefaultencoding(“utf-8”) class demoTest(unittest.TestCase): if __name__ == ‘__main__’: python unintest单元测试框架提供了一整套内置的断言方法。 如果断言失败,则抛出一个AssertionError,并标识该测试为失败状态 如果异常,则当做错误来处理 注意:以上两种方式的区别 如果成功,则标识该测试为成功状态 下面我们看下在unittest框架中定义了哪几类断言方法: 基本的Boolean断言,即:要么True,要么False的验证 简单比较断言,例如比较a,b两个变量的值 复杂断言 基本的断言方法提供了测试结果是True还是False。所有的断言方法都有一个msg参数,如果指定msg参数的值,则将该信息作为失败的错误信息返回。 序号 断言方法 断言描述 1 assertEqual(arg1, arg2, msg=None) 验证arg1=arg2,不等则fail 2 assertNotEqual(arg1, arg2, msg=None) 验证arg1 != arg2, 相等则fail 3 assertTrue(expr, msg=None)

python自动化基础(参数化)

匿名 (未验证) 提交于 2019-12-02 22:51:30
一、创建加法类 #定义一个数学加法类 class Mathmethod(): def add(self,a,b): return(a+b) def sub(self,a,b): return(a-b) import unittest from HTMLTestRunner import HTMLTestRunner from requesttest.math1.Mathmethod import Mathmethod # 引入Mathmethod模块 #import HTMLTestRunnerNew class TestMathmethod(unittest.TestCase): #超继承(既有父类的特性,又有自己的新特性) #子类有跟父类相同的特性,就会覆盖父类的特性 重写 def __init__(self,methodName,a,b,excepted): super(TestMathmethod,self).__init__(methodName)#超继承父类的初始化函数 self.a=a self.b=b self.excepted=excepted def test_method_add(self): res=Mathmethod().add(self.a,self.b) print('两个数值相加结果是',res) self.assertEqual(self

Python学习笔记之测试函数

匿名 (未验证) 提交于 2019-12-02 22:51:30
11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为City, Country 的字符串,如Santiago, Chile。将这个函数存储在一个名为city _functions.py 的模块中。 创建一个名为test_cities.py 的程序,对刚编写的函数进行测试(别忘了,你需要导入模块unittest 以及要测试的函数)。编写一个名为test_city_country()的方法,核实使用类似于'santiago'和'chile'这样的值来调用前述函数时,得到的字符串是正确的。运行test_cities.py,确认测试test_city_country()通过了。 city _functions.py 1 def city_country(city, country): 2 city_name = city + ', ' + country 3 return city_name.title() test_city_country() 1 import unittest 2 from city_function import city_country 3 4 class CityTestCase(unittest.TestCase): 5 """测试city_function.py""" 6 7 def test_city

python+unittets框架

匿名 (未验证) 提交于 2019-12-02 22:51:30
本文来源于: https://www.cnblogs.com/insane-Mr-Li/p/9132406.html 我们整个自动化才是报告的环节基本上分为三个部分: 1.测试用例的准备 2.测试用例的执行 3.测试报告的输出 1.测试用例的准备: 那我们就以搜孤网页做一个简单的用例: from selenium import webdriver import unittest class Case_1(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window()#放大浏览器 self.driver.get("https://www.sogou.com/") def test_001(self): self.driver.find_element_by_link_text('微信').click() self.assertIn(self.driver.title,u'搜狗微信搜索_订阅号及文章内容独家收录,一搜即达') def test_002(self): self.driver.find_element_by_link_text('新闻').click() self.assertIn(self.driver.title,u'搜狗新闻 -

python+uiautomator2自动化测试二

匿名 (未验证) 提交于 2019-12-02 22:51:30
python+uiautomator2自动化测试框架 利用unittest测试生成测试报告 #coding=utf-8 import uiautomator2 as u from time import sleep import unittest from HTMLTestRunner import HTMLTestRunner #usb连接设备 d = u . connect_usb ( '0000' ) class music ( unittest . TestCase ): def start_music ( self ): # 启动App d . app_start ( "com.tencent.qqmusic" ) sleep ( 10 ) # 判断应用是否启动成功 self . assertTrue ( d ( resourceId = "com.tencent.qqmusic:id/ll_layout" ). exists ) def test1 ( self ): #进入音乐馆 d ( description = "返回" ). click () sleep ( 5 ) self . assertTrue ( d ( text = "音乐馆" ). exists ) d . swipe ( 500 , 100 , 500 , 10 , 5 ) def test2 (

Python 笔记 :12 代码测试

匿名 (未验证) 提交于 2019-12-02 22:51:30
代码测试:是通过代码测试函数,来把执行的结果,和用户预设的代码内容,进行比较! 格式:   import unittest #定义的类,要继承unittest.TestCase类   class Test_a(unittest.TestCase):     ...... #方法名,必须以test开头,下面的unittest.main( ),才可自动执行代码测试,否则不会执行     def test_first_last_name(self): #这里会把函数的返回值,和用户预设的结果,进行比较,如果相同,则通过测试,否则报错,并提示测试失败       self.assertEqual(代码执行的结果或调用的函数(有返回值),用户预设的结果(如:'Jimm Green')   unittest.main( ) 例子:  #判断用户输入的格式,是否符合格式要求 import unittest #定义一个函数,用来返回全部姓名:def get_formatted_name(first, last,middle=''): """Generate a neatly formatted full name.""" #如果有middle有值,则输出middle,否则不输出middle if middle: full_name = first + ' ' + middle + ' ' +

unittest 断言方法---(虫师《selenium3自动化测试实战--基于Python语言笔记31》)

匿名 (未验证) 提交于 2019-12-02 22:51:30
TestCase提供的测试结果的断言方法 方法 检查 版本 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 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 (

python Mock运用

匿名 (未验证) 提交于 2019-12-02 22:51:30
一.Mock概念 unittest.mock是一个用于在Python中进行单元测试的库,Mock翻译过来就是模拟的意思,顾名思义这个库的主要功能是模拟一些东西。 它的主要功能是使用mock对象替代掉指定的Python对象,以达到模拟对象的行为。 二.Mock的作用 1.如果你在写一个接口自动化,然后需要A接口返回结果给你的B接口使用,那么你就可以使用Mock了。 2.第三方接口依赖,在做接口自动化的时候,有时候需要调用第三方的接口,但是别人公司的接口服务不受你的控制,有可能别人提供的测试环境今天服务给你开着,别人就关掉了, 给自动化接口测试带来很多的麻烦,此时就可以自己写一个mock-server来模拟接口的返回数据。 三.Mock的导入 Mock在py3.0后就已经放在unittest框架里面了,所以直接from unittest import mock 就行了 四.实例演示 mock_data.py 1 def register (): 2 '''假设这里是一个注册的功能,未开发完 3 注册成功返回:{' status ': 1, ' code ': ' 10001 ', ' data ': None, ' msg ': ' 注册成功 '} 4 注册失败返回:{' status ': 0, ' code ': ' 20110 ', ' data ': None, ' msg '