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. Python入门-从HelloWorld开始

    前言 最近在招聘网上看了许多公司的招聘要求,发现很多公司希望求职者能会Python,特别是一些自动化测试的职位,以前对Python只是介于听说或是一些简单的了解,所以既然市场有需求,那么我们就来学习一 ...

  2. Misha, Grisha and Underground CodeForces - 832D (倍增树上求LCA)

    Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...

  3. ajax相关问题

    1.contentType和dataType contentType 主要设置你发送给服务器的数据格式 dataType设置你收到服务器数据的格式(如text,json等),最常用的为json. 2. ...

  4. CRM系统(第二部分)

      阅读目录 一.讲师与学生简介 二. 初始化 ,studyrecord, 三.初始化 course_record:批量生成学习记录 四. 考勤  url跳转 五.录入成绩 六.highcharts表 ...

  5. http/https与websocket的ws/wss的关系以及通过Nginx的配置

    http/https与websocket的ws/wss的关系 - 哒哒哒 - CSDN博客 https://blog.csdn.net/Garrettzxd/article/details/81674 ...

  6. JEECG DataGridColumn dictionary使用问题

    <t:dgCol title="线索所属人"  field="ownerId"  query="true"  queryMode=&q ...

  7. Spring中RedirectAttributes的用法

    RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...

  8. ELK日志系统+x-pack安全验证

    根据之前已经搭好的ELK系统,现在加一个x-pack插件上去,不然谁拿到ip和端口都可以访问elasticsearch和kibana. 要的效果如下:打开kibana界面的时候要让其输入用户名密码才能 ...

  9. java.lang.NoClassDefFoundError: org/apache/log4j/Priority的问题解决

    在pom 文件中添加 <dependency> <groupId>log4j</groupId> <artifactId>log4j</artif ...

  10. java学习之—栈匹配字符串符号

    /** * 栈 * Create by Administrator * 2018/6/11 0011 * 上午 10:20 **/ public class StackR { private int ...