重复的全局变量,并在Python单元测试中尝试



我已经开始使用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()

根本不需要全局变量。你在班上;在整个过程中使用self.res

这正是 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.

顺便说一句,为什么要使用全局变量?他们没有必要。实际上,使用全球群体很少有有效的理由。此外,需要隔离和独立的测试,使用全局变量违反了该规则。

感谢上面的提示。我将代码减少到了这个版本,希望看起来更干净,更标准:

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()

最新更新