【问题标题】:repetitive global variable and try except in python unit tests重复的全局变量并尝试在 python 单元测试中除外
【发布时间】:2017-09-29 08:56:59
【问题描述】:

我已经开始使用 Python Selenium 并编写了如下所示的脚本。

这会打印一个链接到失败的测试的返回码(test01、test02、test03、..)。

忽略每个测试都在检查同一件事。

我只是想了解是否有更简洁的方法来编写测试,因为每个测试都重复声明global res,然后有一个try/except 块。

有人可以就如何改进这一点提供一些建议吗?

# global variable for return code. Zero is success.
res=0

@atexit.register
def send_health():
    print ("res=%s") % res

class Login(unittest2.TestCase):
    @classmethod
    def setUpClass(inst):
        binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
        inst.driver = webdriver.Firefox(firefox_binary=binary)
        inst.base_url = "https://stackoverflow.com"

    def test01(self):
        global res
        driver = self.driver
        try:
            self.assertEqual("Ask a Question", driver.title)
        except Exception,e:
            print ("Exception: %s" % e)
            driver.save_screenshot('screenshot_test01.png')
            res=1
            return

    def test02(self):
        global res
        driver = self.driver
        try:
            self.assertEqual("Ask a Question", driver.title)
        except Exception,e:
            print ("Exception: %s" % e)
            driver.save_screenshot('screenshot_test02.png')
            res=2
            return

    def test03(self):
        global res
        driver = self.driver
        try:
            self.assertEqual("Ask a Question", driver.title)
        except Exception,e:
            print ("Exception: %s" % e)
            driver.save_screenshot('screenshot_test03.png')
            res=3
            return

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

【问题讨论】:

    标签: python unit-testing selenium


    【解决方案1】:

    这里根本不需要全局变量。你在一个班级里;始终使用self.res

    【讨论】:

    • 谢谢丹尼尔,我会试试这个。
    • 当它调用send_health退出函数print ("res=%s") % self.resNameError: global name 'self' is not defined时会中断
    【解决方案2】:

    这正是setUp 实例方法的用途,非常类似于每个测试类运行一次的setUpClass 方法。

    def setUp(self):
        # this code will be executed before each and every test
        self.res = 0
    
    def tearDown(self):
        # this code will be executed post each and every test.
    

    顺便问一下,你为什么使用全局变量?不需要他们。事实上,很少有使用全局变量的有效理由。此外,测试需要隔离和独立,使用全局变量会违反该规则。

    【讨论】:

    • 感谢您的快速回复。我正在使用setUpClasstearDownClass,因为测试需要登录网站,然后点击旅程。
    • @Edward 我对使用setUpClass 没有任何问题。但是,您不需要全局 res 值。测试需要隔离,因此完全不建议在它们之间共享值。
    【解决方案3】:

    感谢以上提示。我已将代码缩减为这个版本,希望看起来更干净、更标准:

    class Login(unittest2.TestCase):
    
        @classmethod
        def handleError(self, e, res):
            print ("Test failed with exception: %s" % e)
            self.result = res
            sys.exit()
    
        @classmethod
        def setUpClass(self):
            binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe')
            self.driver = webdriver.Firefox(firefox_binary=binary)
            self.base_url = "https://stackoverflow.com"
            self.result = 0
    
        def test01(self):
            driver = self.driver
            try:
                self.assertEqual("Ask a Question", driver.title)
            except Exception,e:
                self.handleError(e, 1)
    
        def test02(self):
            driver = self.driver
            try:
                self.assertEqual("Ask a Question", driver.title)
            except Exception,e:
                self.handleError(e, 2)
    
        def test03(self):
            driver = self.driver
            try:
                self.assertEqual("Ask a Question", driver.title)
            except Exception,e:
                self.handleError(e, 3)
    
        @classmethod
        def tearDownClass(self):
            self.driver.quit()
            print ("res=%s") % self.result
    
    if __name__ == "__main__":
        unittest2.main()
    

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      相关资源
      最近更新 更多