What's the matter with this code section?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny Lee

    #1

    What's the matter with this code section?

    Here is the source:

    #! /bin/python

    #@brief This is a xunit test framework for python, see TDD for more
    details

    class TestCase:
    def setUp(self):
    print "setUp in TestCase"
    pass
    def __init__(self, name):
    print "__init__ in TestCase"
    self.name = name
    def run(self):
    print "run in TestCase"
    self.setUp()
    method = getattr(self, self.name)
    method()

    class WasRun(TestCase ):
    def __init__(self, name):
    print "__init__ in WasRun"
    self.wasRun = None
    TestCase.__init __(self, name)
    def testMethod(self ):
    print "testMethod in WasRun"
    self.wasRun = 1
    def run(self):
    print "run in WasRun"
    method = getattr(self, self.name)
    method()
    def setUp(self):
    print "in setUp of WasRun"
    self.wasSetUp = 1

    class TestCaseTest(Te stCase):
    def testRunning(sel f):
    print "testRunnin g in TestCaseTest"
    test = WasRun("testMet hod")
    assert(not test.wasRun)
    test.run()
    assert(test.was Run)
    def testSetUp(self) :
    print "testSetUp in TestCaseTest"
    test = WasRun("testMet hod")
    test.run()
    assert(test.was SetUp)

    # the program starts here
    print "starts TestCaseTest(\" testRunning\"). run()"
    TestCaseTest("t estRunning").ru n()
    print "starts TestCaseTest(\" testSetUp\").ru n()"
    TestCaseTest("t estSetUp").run( )



    And here is the result running under cygwin:

    $ ./xunit.py
    starts TestCaseTest("t estRunning").ru n()
    __init__ in TestCase
    run in TestCase
    setUp in TestCase
    testRunning in TestCaseTest
    __init__ in WasRun
    __init__ in TestCase
    run in WasRun
    testMethod in WasRun
    starts TestCaseTest("t estSetUp").run( )
    __init__ in TestCase
    run in TestCase
    setUp in TestCase
    testSetUp in TestCaseTest
    __init__ in WasRun
    __init__ in TestCase
    run in WasRun
    testMethod in WasRun
    Traceback (most recent call last):
    File "./xunit.py", line 51, in ?
    TestCaseTest("t estSetUp").run( )
    File "./xunit.py", line 16, in run
    method()
    File "./xunit.py", line 45, in testSetUp
    assert(test.was SetUp)
    AttributeError: WasRun instance has no attribute 'wasSetUp'

  • Simon Brunning

    #2
    Re: What's the matter with this code section?

    On 24 Aug 2005 00:18:00 -0700, Johnny Lee <johnnyandfiona @hotmail.com> wrote:[color=blue]
    > AttributeError: WasRun instance has no attribute 'wasSetUp'[/color]

    It means exactly what it says. The last line of your TestCaseTest
    classes testSetUp method refers to test.wasSetUp. So far as I can see,
    test is an instance of your WasRun class, and I don't see a wasSetUp
    attribute being set anywhere.

    --
    Cheers,
    Simon B,
    simon@brunningo nline.net,

    Comment

    • bruno modulix

      #3
      Re: What's the matter with this code section?

      Johnny Lee wrote:[color=blue]
      > Here is the source:
      >[/color]
      (snip)[color=blue]
      > class TestCaseTest(Te stCase):
      > def testRunning(sel f):
      > print "testRunnin g in TestCaseTest"
      > test = WasRun("testMet hod")
      > assert(not test.wasRun)
      > test.run()
      > assert(test.was Run)
      > def testSetUp(self) :
      > print "testSetUp in TestCaseTest"
      > test = WasRun("testMet hod")
      > test.run()[/color]
      Shouldn't it be test.setUp() instead ?

      Unless the problem is here:

      ### TestCase.run() calls self.setUp()
      class TestCase:
      (snip)
      def run(self):
      print "run in TestCase"
      self.setUp()
      method = getattr(self, self.name)
      method()

      ### WasRun.run() doesn't call self.setUp()
      class WasRun(TestCase ):
      (snip)
      def run(self):
      print "run in WasRun"
      method = getattr(self, self.name)
      method()
      def setUp(self):
      print "in setUp of WasRun"
      self.wasSetUp = 1


      BTW, if the only reason for overloading WasRun.run() is to print "run in
      WasRun", you can also modify parent's class run() method so it display
      the effective class name, not an hard-coded one:

      class TestCase:
      (snip)
      def run(self):
      print "run in %s" % self.__class__. __name__
      self.setUp()
      method = getattr(self, self.name)
      method()

      and then remove run() from WasRun.

      There should be a simple solution to 'aspect' the debug/trace stuff with
      a decorator, like:

      def traced(func):
      def _wrapper(self, *args, **kwargs):
      print "%s method in %s class" % (func.func_name ,
      self.__class__. __name)
      return func(self, *args, **kwargs)

      return _wrapper(func)

      class TestCase:
      @traced
      def run(self):
      self.setUp()
      method = getattr(self, self.name)
      method()

      (not tested, and I don't have much experience with decorators...)

      --
      bruno desthuilliers
      ruby -e "print 'onurb@xiludom. gro'.split('@') .collect{|p|
      p.split('.').co llect{|w| w.reverse}.join ('.')}.join('@' )"
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      Working...