admin管理员组

文章数量:1130349

前言

unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间。

于是就想是不是可以只打开一次浏览器,执行完用例再关闭呢?这就需要用到装饰器(@classmethod)来解决了。

装饰器

用setUp与setUpClass区别

  • setup():每个测试case运行前运行
  • teardown():每个测试case运行完后执行
  • setUpClass():必须使用@classmethod 装饰器,所有case运行前只运行一次
  • tearDownClass():必须使用@classmethod装饰器,所有case运行完后只运行一次

@是修饰符,classmethod是python里的类方法

执行顺序

1.用类方法写几个简单case

#!/usr/bin/python3

# @Time    : 2022/8/19 18:19
# @Author  : 技术分享群 721945856

import unittest
import time

class Test(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    print "开始!"

  @classmethod
  def tearDownClass(cls):
    time.sleep(1)
    print "结束"

  def test01(self):
    print "执行用例1"

  def test03(self):
    print "执行用例3"

  def test02(self):
    print "执行用例2"

  def addtest(self):
    print "add方法"

if __name__ == "__main__":
  unittest.main()

从执行结果可以看出,前置和后置在执行用例前只执行了一次。

开始

执行用例1

执行用例2

执行用例3

…结束

selenium实例

1.可以把打开浏览器操作放到前置setUpClass(cls)里,这样就可以实现打开一次浏览器,执行多个case了

#!/usr/bin/python3

# @Time    : 2022/8/19 18:25
# @Author  : 技术分享群 721945856

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import unittest

class BolgHome(unittest.TestCase):
  u'''博客首页'''
  @classmethod
  def setUpClass(cls):
    cls.driver = webdriver.Firefox()
    url = "https://blog.csdn/weixin_56502375?type=lately"
    cls.driver.get(url)
    cls.driver.implicitly_wait(30)

  @classmethod
  def tearDownClass(cls):
    cls.driver.quit()

  def test_01(self):
    u'''验证元素存在:博客园'''
    locator = ("id", "blog_nav_sitehome")
    text = u"博客园"
    result = EC.text_to_be_present_in_element(locator, text)(self.driver)
    self.assertTrue(result)

  def test_02(self):
    u'''验证元素存在:首页'''
    locator = ("id", "blog_nav_myhome")
    text = u"首页"
    result = EC.text_to_be_present_in_element(locator, text)(self.driver)
    self.assertTrue(result)

if __name__ == "__main__":
  unittest.main()

现在我邀请你进入我们的软件测试学习交流群:【 721945856 】,备注“csdn”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。

前言

unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间。

于是就想是不是可以只打开一次浏览器,执行完用例再关闭呢?这就需要用到装饰器(@classmethod)来解决了。

装饰器

用setUp与setUpClass区别

  • setup():每个测试case运行前运行
  • teardown():每个测试case运行完后执行
  • setUpClass():必须使用@classmethod 装饰器,所有case运行前只运行一次
  • tearDownClass():必须使用@classmethod装饰器,所有case运行完后只运行一次

@是修饰符,classmethod是python里的类方法

执行顺序

1.用类方法写几个简单case

#!/usr/bin/python3

# @Time    : 2022/8/19 18:19
# @Author  : 技术分享群 721945856

import unittest
import time

class Test(unittest.TestCase):
  @classmethod
  def setUpClass(cls):
    print "开始!"

  @classmethod
  def tearDownClass(cls):
    time.sleep(1)
    print "结束"

  def test01(self):
    print "执行用例1"

  def test03(self):
    print "执行用例3"

  def test02(self):
    print "执行用例2"

  def addtest(self):
    print "add方法"

if __name__ == "__main__":
  unittest.main()

从执行结果可以看出,前置和后置在执行用例前只执行了一次。

开始

执行用例1

执行用例2

执行用例3

…结束

selenium实例

1.可以把打开浏览器操作放到前置setUpClass(cls)里,这样就可以实现打开一次浏览器,执行多个case了

#!/usr/bin/python3

# @Time    : 2022/8/19 18:25
# @Author  : 技术分享群 721945856

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import unittest

class BolgHome(unittest.TestCase):
  u'''博客首页'''
  @classmethod
  def setUpClass(cls):
    cls.driver = webdriver.Firefox()
    url = "https://blog.csdn/weixin_56502375?type=lately"
    cls.driver.get(url)
    cls.driver.implicitly_wait(30)

  @classmethod
  def tearDownClass(cls):
    cls.driver.quit()

  def test_01(self):
    u'''验证元素存在:博客园'''
    locator = ("id", "blog_nav_sitehome")
    text = u"博客园"
    result = EC.text_to_be_present_in_element(locator, text)(self.driver)
    self.assertTrue(result)

  def test_02(self):
    u'''验证元素存在:首页'''
    locator = ("id", "blog_nav_myhome")
    text = u"首页"
    result = EC.text_to_be_present_in_element(locator, text)(self.driver)
    self.assertTrue(result)

if __name__ == "__main__":
  unittest.main()

现在我邀请你进入我们的软件测试学习交流群:【 721945856 】,备注“csdn”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。

本文标签: 我不想每条就可以浏览器测试