2014-03-29 22 views
0

我嘗試在通常的類中使用assertEqual並且無法從unittest.TestCase調用方法Selenium python如何從unittest.TestCase中使用assertEqual方法「沒有這樣的測試方法在<class'unittest.case.TestCase'>」

class MyPages(unittest.TestCase): 

     @classmethod 
     def setUpClass(cls): 
      basetest.BaseTest().open_browser('firefox') 
      basetest.BaseTest().login() 


     def testCreateFolder(self): 
      print "111111111" 

     def testCreateFolder1(self): 
      print "222222222" 

     @classmethod 
     def tearDownClass(cls): 
      basetest.BaseTest().close_browser() 

而在我的BaseTest中,我想使用文本斷言進行登錄。

class BaseTest(): 

     def open_browser(self, browser): 
      self.driver = config.browser[browser] 
      global driver 
      driver = self.driver 
      driver.get(config.url) 


     def login(self): 
      # Go to authorisation page 
      driver.find_element_by_xpath(link.header["Login_button"]).click() 
      # Get text from LOGIN label and assert it with expected text 
      login_text = driver.find_element_by_xpath(link.author_popup["Login_label"]) 
      login_text.get_attribute("text") 
      print login_text.text 
      unittest.TestCase().assertEqual(1, 1, "helllllllo") 
      unittest.TestCase().assertEqual(login_text.text, text.author_popup["Login"], 
           "Wrong label on log in auth popup. Expected text:") 

因此,我有以下幾點:

Error 
    Traceback (most recent call last): 
     File "D:\python\PD_Tests\pages\my_pages.py", line 17, in setUpClass 
     basetest.BaseTest().login() 
     File "D:\python\PD_Tests\tests\basetest.py", line 25, in login 
     unittest.TestCase().assertEqual(1, 1, "helllllllo") 
     File "C:\Python27\lib\unittest\case.py", line 191, in __init__ 
     (self.__class__, methodName)) 
    ValueError: no such test method in <class 'unittest.case.TestCase'>: runTest 

我可以使用assertEqual便方法,我的方法,如果我的課是不是unittest.TestCase生成?

回答

1

我認爲有一種方法可以做你想做的事,但這有點破解。

TestCase類的構造函數將方法名稱作爲參數,此參數的默認值爲"runTest"。此構造函數的文檔字符串如下所示:

創建一個類的實例,該類將在執行時使用命名的測試方法。如果實例沒有指定名稱的方法,則引發ValueError。

希望這應該解釋您看到的錯誤消息。

如果你想創建一個TestCase只是使用斷言方法,你可以改爲傳入其他方法的名稱,如__str__。這將讓你過去的構造做了檢查:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from unittest import TestCase 
>>> t = TestCase("__str__") 
>>> t.assertEqual(3, 5) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\Python27\lib\unittest\case.py", line 511, in assertEqual 
    assertion_func(first, second, msg=msg) 
    File "C:\Python27\lib\unittest\case.py", line 504, in _baseAssertEqual 
    raise self.failureException(msg) 
AssertionError: 3 != 5 
>>> t.assertEqual(3, 3) 
>>> 

只要你不嘗試運行您的測試用例,這不應該是一個問題。

+0

現在我只是使用「if」而不是assertEqual。唯一的問題是,如果我在IF中沒有相同的文本,我的項目將運行下一步,斷言將停止測試。但現在對我來說沒問題。感謝您的回答。 – Michael

1

是否有特殊原因不是到子類BaseTestunittest.TestCase

我的解決辦法是有一個SeleniumTestcase類,它繼承unittest.TestCase並提供setUptearDown方法。只要確保在創建SeleniumTestcase實例時知道您的config

相關問題