Asserts in Python 2.7 not working for me example a

2019-07-16 11:21发布

问题:

I have python 2.7 installed on my Mac. (verified by running python -v in terminal) When I try to use any of the new 2.7 assert methods I get AtributeError. I have looked at http://docs.python.org/2/library/unittest.html and can't find anything. The Asserts that come with 2.6 all work Ex Self.AssertEqual (x,x)

Below is my code. When I call the assertIn I get the below message.

 * 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'

below is my code

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

回答1:

I suspect that your code is not running in the version of Python that you think it is. If you add the following test case:

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

If that test case fails, then you're not running on a version of Python new enough to contain self.assertIn().