使用python的单元测试的聚合测试正在运行,没有测试运行器

时间:2012-12-20 23:03:22

标签: python unit-testing selenium webdriver

我在使用python中的unit-test运行聚合测试的selenium测试套件时遇到了问题。

下面的代码是在没有调用testRunner的情况下从另一个模块执行测试。

当我尝试在调试模式下执行时,控件在执行类定义行后很快就传递给了pydev_runfiles,并最终在另一个模块(gmailbutton)中执行了测试。

import unittest
from selenium import webdriver
from gmailbutton import gmailButton

class runner():
    def runner1(self):
        suite = unittest.TestSuite()
        suite.addTest(gmailButton)
        return suite

根据文档(http://docs.python.org/2/library/unittest.html),上面的代码只需要将测试用例添加到套件中,测试应该已经执行了

unittest.TextTestRunner(verbosity=2).run(suite)

这里没有发生。

gmailButton的测试代码在这里

import unittest
from selenium import webdriver


class gmailButton(unittest.TestCase):

    global browser

    def test_gmailButton(self):

        browser = webdriver.Firefox()

        try:
            browser.get("http://www.gmail.com")           
            browser.find_element_by_id("Email").send_keys("abcd")
            browser.find_element_by_id("Passwd").send_keys("123445")
            browser.find_element_by_id("signIn").click()

        except Exception as e:
            raise
            print e

        finally:
            browser.close()

更新:

这是我从eclipse执行的确切代码。

import unittest
from selenium import webdriver
from gmailbutton import gmailButton
from pyUnitExercise import exercise1

class runner():
    def runner1(self):
        suite = unittest.TestSuite()
        suite.addTest(gmailButton)
        return suite

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(runner.suite)

我对此代码的期望是执行gmailButton测试用例和NOT exercise1,它只是导入而未添加到测试套件中。我不知道为什么它执行刚刚导入的测试而没有添加到测试套件中。

2 个答案:

答案 0 :(得分:1)

如果你正在使用PyDev,你可能实际上并没有使用python的stdlib unittest。它使用nose和/或py.test:

http://pydev.org/manual_adv_pyunit.html

这两个库都通过内省搜索测试;您不需要向测试套件添加显式条目。如果没有测试运行时的输出,很难判断这是否真的发生了什么。

答案 1 :(得分:0)

我优先使用nose进行单元测试,但这是一种似乎对我有用的方法:

>>> import unittest
>>> class gmailButton(unittest.TestCase):
    def test_gmailButton(self):
        pass


>>> suite = unittest.TestSuite()
>>> loader = unittest.TestLoader()
>>> tests = loader.loadTestsFromTestCase(gmailButton)
>>> suite.addTest(tests)
>>> unittest.TextTestRunner(verbosity=2).run(suite)
test_gmailButton (__main__.gmailButton) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.047s

OK
<unittest.runner.TextTestResult run=1 errors=0 failures=0>
相关问题