Unnitest总结

第一点,setUp和tearDown方法

每次执行test开头的用例都会执行setUp和tearDown方法

如:

     import unittest

 class Mydemo(unittest.TestCase):
def setUp(self):
print('调用setUp方法')
self.a = 1
def test1(self):
print("i am test1 the value of a is {}".format(self.a))
def test2(self):
print("i am test2 the value of a is {}".format(self.a))
def test3(self):
print("i am test3 the value of a is {}".format(self.a))
def tearDown(self):
print("调用tearDown方法")
if __name__ == '__main__':
unittest.main()

结果:

第二点: setUpClass和tearDownClass方法

如:

     import unittest
class Mydemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("调用setUpClass\n")
def test1(self):
print("i am test1")
def test2(self):
print("i am test2")
@classmethod
def tearDownClass(cls):
print("调用tearDownClass")
if __name__ == '__main__':
unittest.main()

结果:

第三点:定义全局的属性,可以放到setUpClass中testCase中均可去调用

如:

     class NavigationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
cls.driver.get('https://www.baidu.com/')
driver = cls.driver #定义在全局中
cls.search_field = driver.find_element_by_name('wd') #定义在全局中 def testBrowserNavigation(self):
self.search_field.clear()
print('执行test1') self.search_field.send_keys('圣女果')
self.search_field.submit()
time.sleep(1) self.assertEqual('圣女果_百度搜索',self.driver.title) self.driver.back()
self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
def testBrowserNavigation2(self):
driver = self.driver
search_field = driver.find_element_by_name('wd')
search_field.clear()
print('执行test2') search_field.send_keys('璎珞')
search_field.submit()
time.sleep(1)
@classmethod
def tearDownClass(cls):
driver = cls.driver
time.sleep(10)
driver.quit() if __name__ == "__main__":
unittest.main()

第四点:testCase之间的执行顺序按照默认是以首字母排序的

执行顺序test1,test10,....test18,test2,..........test9

Unnitest总结

第一点,setUp和tearDown方法

每次执行test开头的用例都会执行setUp和tearDown方法

如:

l   import unittest

class Mydemo(unittest.TestCase):
    def setUp(self):
        print('调用setUp方法')
        self.a =
1
    def test1(self):
        print("i am test1 the value of a is {}".format(self.a))
    def test2(self):
        print("i am test2 the value of a is {}".format(self.a))
    def test3(self):
        print("i am test3 the value of a is {}".format(self.a))
    def tearDown(self):
        print("调用tearDown方法")
if __name__ == '__main__':
    unittest.main()

结果:

第二点: setUpClass和tearDownClass方法

如:

l   import unittest
class Mydemo(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("调用setUpClass\n")
    def test1(self):
        print("i am test1")
    def test2(self):
        print("i am test2")
    @classmethod
    def tearDownClass(cls):
        print("调用tearDownClass")
if __name__ == '__main__':
    unittest.main()

结果:

第三点:定义全局的属性,可以放到setUpClass中testCase中均可去调用

如:

l   class NavigationTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()
        cls.driver.get('https://www.baidu.com/')
        driver = cls.driver #定义在全局中
        cls.search_field = driver.find_element_by_name('wd') #定义在全局中     def testBrowserNavigation(self):
        self.search_field.clear()
        print('执行test1')         self.search_field.send_keys('圣女果')
        self.search_field.submit()
        time.sleep(1)         self.assertEqual('圣女果_百度搜索',self.driver.title)         self.driver.back()
        self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
    def testBrowserNavigation2(self):
        driver = self.driver
        search_field = driver.find_element_by_name('wd')
        search_field.clear()
        print('执行test2')         search_field.send_keys('璎珞')
        search_field.submit()
        time.sleep(1)
    @classmethod
    def tearDownClass(cls):
        driver = cls.driver
        time.sleep(10)
        driver.quit() if __name__ == "__main__":
    unittest.main()

第四点:testCase之间的执行顺序按照默认是以首字母排序的

执行顺序test1,test10,....test18,test2,..........test9

Unnitest测试框架总结的更多相关文章

  1. phpunit 测试框架安装

    PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...

  2. 某互联网后台自动化组合测试框架RF+Sikuli+Python脚本

    某互联网后台自动化组合测试框架RF+Sikuli+Python脚本 http://www.jianshu.com/p/b3e204c8651a 字数949 阅读323 评论1 喜欢0 一.**Robo ...

  3. selenium测试框架使用xml作为对象库

    之前已经写过一篇: selenium测试框架篇,页面对象和元素对象的管理 上次使用的excel作为Locator对象管理,由于excel处理不够方便,有以下缺点: 不能实现分page 加载Locato ...

  4. selenium 测试框架中使用grid

    之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...

  5. selenium测试框架篇,页面对象和元素对象的管理

    前期已经做好使用Jenkins做buildhttp://www.cnblogs.com/tobecrazy/p/4529399.html 做自动化框架,不可避免的就是对象库. 有一个好的对象库,可以让 ...

  6. Junit测试框架 Tips

    关于Junit测试框架使用的几点总结: 1.Junit中的测试注解: @Test →每个测试方法前都需要添加该注解,这样才能使你的测试方法交给Junit去执行. @Before →在每个测试方法执行前 ...

  7. Python几种常用的测试框架

    一.测试的常用规则 一个测试单元必须关注一个很小的功能函数,证明它是正确的: 每个测试单元必须是完全独立的,必须能单独运行.这样意味着每一个测试方法必须重新加载数据,执行完毕后做一些清理工作.通常通过 ...

  8. 测试框架Mocha与断言expect

    测试框架Mocha与断言expect在浏览器和Node环境都可以使用除了Mocha以外,类似的测试框架还有Jasmine.Karma.Tape等,也很值得学习. 整个项目源代码: 为什么学习测试代码? ...

  9. 在测试框架中使用Log4J 2

    之前的测试框架:http://www.cnblogs.com/tobecrazy/p/4553444.html 配合Jenkins可持续集成:http://www.cnblogs.com/tobecr ...

随机推荐

  1. flask 跨域请求

    Flask中,跨域请求主要有两种方式: 1.在响应头信息中添加允许跨域 如下,使用装饰器app.after_request(我这里的web是定义的蓝图),这样在每次请求后,加入header 2.使用第 ...

  2. python中各个response使用

    Python django中我们经常用的response有django中的 JsonResponse, HttpResponse,还有DRF中的Response 在使用的时候,经常会不知道如何什么时候 ...

  3. Vue(二)基础

    01-vue的起步 1.引包 a) 直接下载,并用<script>标签引入 b) CDN方式引入: <script src="https://cdn.bootcss.com ...

  4. webapack

    webpack  就是一个前端资源加载.打包工具. 核心思想:会根据(js css less文件)模块依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源,减少页面请求. wapack ...

  5. Consecutive Subsequence CodeForces - 977F (map优化DP)·

    You are given an integer array of length nn. You have to choose some subsequence of this array of ma ...

  6. 初次使用git上传代码到github远程仓库

    https://blog.csdn.net/loner_fang/article/details/80488385 2018年05月28日 21:02:31 蒲公英上的尘埃 阅读数:697 因为最近在 ...

  7. iOS-拍照后裁剪,不可拖动照片的问题

    2016.07.08 15:04* 字数 1837 阅读 6066评论 6喜欢 26赞赏 1 问题 在项目中,选择照片或拍照的功能很长见,由于我之前采用系统自带的UIimagePickViewCont ...

  8. Use the Microsoft Symbol for VS and Windbg

    快捷方式mklink的远程符号由于所有者权限问题,链接到本地可能造成不能使用, 或每次都需要重新下载, 1.环境变量中没有设置_NT_SYMBOL_PATH的值 2.windbg快捷方式中也没有设置- ...

  9. 开发神器之phpstorm破解与日常使用

    PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查. PhpStorm可随时帮助用 ...

  10. Webbench、ab命令:做压力测试的工具和性能的监控工具

    DDOS攻击:???DDOS概述:分布式拒绝服务(DDoS:Distributed Denial of Service)攻击,指借助于客户/服务器技术,将多个计算机联合起来作为攻击平台,对一个或多个目 ...