Skip to main content
 首页 » 编程设计

python之Python 2.7 中的断言对我不起作用示例 assertIn

2025年05月04日12yxwkf

我在我的 Mac 上安装了 python 2.7。 (通过在终端中运行 python -v 进行验证) 当我尝试使用任何新的 2.7 断言方法时,我得到了 AtributeError。我看过http://docs.python.org/2/library/unittest.html找不到任何东西。 2.6 附带的断言都可以工作 Ex Self.AssertEqual (x,x)

下面是我的代码。当我调用 assertIn 时,我收到以下消息。

 * Test complete (ERROR):  test012_Add_user2 (__main__.TestSuite) 
 
 
(<type 'exceptions.AttributeError'>, AttributeError("'TestSuite' object has no attribute 'assertIn'",), <traceback object at 0x3>) 
Traceback (most recent call last): 
  File "/Users/drlazor/Documents/AIM/7-16/oscar/qa/clients/TRAVOLTA/scripts_iphone/Add_Favorites.sikuli/Add_Favorites.py", line 112, in test012_Add_user2 
    self.assertIn (foo, foo2) 
AttributeError: 'TestSuite' object has no attribute 'assertIn' 

下面是我的代码

import unittest 
import datetime 
from sikuli import * 
 
scriptName = os.path.basename(sys.argv[0]) 
addImagePath( os.path.join("scripts", scriptName) ) 
 
class TestSuite (unittest.TestCase): 
 
 
      #Need to add some Iphone clearing of favorites 
 
   def test001_start_IPhone(self): 
      logger.info (" Start aim iphone application") 
      showEnvironmentSettings() 
      a.startApp() 
      version = a.login(username1, password) 
      myTest.results['deviceVersion'] = version 
      wait(2) 
 
 
   def test012_Add_user2(self): 
      logger.info  ("Add User 2 to the Favorites") 
      a.mFriends_Favorites_addNewFavs((username3[-3:])) 
      Fav=a.mFriends_Favorites_getAllFavs() 
      logger.info("assertin is tried....  will see") 
      #assertIn(Fav, Fav2) 
      self.assertIn ("foo", "foo2") #foo 
      for item in Fav: 
        logger.info (item) 
        logger.info ("HSJDKSKJDHSAKJDHSAKJDHSA  LOOOP 2") 
        if (username3[-3:])not in item: 
          logger.info(item) 
          logger.info(username3[-3:]) 
          logger.info ("dkfjlfjskl  FOUND 062") 
          self.assert ("The screenName %s was not added to the list" %username3) 
 
   def test016_Ck_Favorite_presence(self): 
      pres=a.mFriends_Favorites_checkUserPresence(username3[-3:]) 
      self.assertEqual( pres, 'Offline' ) 
      logger.info("Presence state is  '%s'" %pres) 
 
 
if __name__ == '__main__': 
   logger.info("Running test: "+scriptName) 
   a = IPHONE() 
   b = BROWSER() 
   c = SAWS() 
 
   myTest = TestSuiteRunner(TestSuite, scriptName, testDescription, device=device) 
   myTest.runtest() 

请您参考如下方法:

我怀疑您的代码没有在您认为的 Python 版本中运行。如果添加以下测试用例:

def test_python_version(self): 
    import sys 
    assert sys.version_info[0:3] >= (2,7,0) 

如果该测试用例失败,则说明您运行的 Python 版本不够新,无法包含 self.assertIn()。