Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How about "pure virtual methods"?

Reply
Thread Tools

Re: How about "pure virtual methods"?

 
 
Jp Calderone
Guest
Posts: n/a
 
      12-22-2004
On Wed, 22 Dec 2004 02:27:35 +0200, Noam Raphael <> wrote:
>Jeff Shannon wrote:
> > In the context of changing an existing interface, a unit-testing
> > scenario would mean that, instead of installing a "pure virtual" method
> > on a base class, you'd change the unit-tests to follow the new
> > specification, and then write code that would pass the unit tests. If
> > you are subclassing from a common base, then you'd only need to change
> > the unit test for that common base class (presuming that all derived
> > classes would run those unit tests as well).
> >

> The problem is that I couldn't write a general unit test, since the base
> class wasn't instantiable - there wasn't even any point in making it
> instantiable, since every subclass was constructed with different
> argument definition. They were just required to provide some methods
> (check whether they contain an object, for example) - I don't know how
> to write a unit test for such a base class, or what does it mean. (Well,
> it may mean: check whether all the required methods exist, but come on -
> that's exactly the idea of my suggestion. There's no point in having to
> write the list of required methods another time).


from harness import TestCase

class FoobarTestCase(TestCase):
def instanceFactory(self):
raise NotImplementedError()

def testBazMethod(self):
inst = self.instanceFactory()
self.assertEquals(inst.baz(), 'baz')
...

class QuuxTestCase(FoobarTestCase):
def instanceFactory(self):
return Quux(x=y, a=b)

class WibbleTestCase(FoobarTestCase):
def instanceFactory(self):
return Wibble(1, 2, 3)

This lets you avoid duplicate test code as well as easily test
new concrete implementations. It's an ideal approach for frameworks
which mandate application-level implementations of a particular
interface and want to ease the application developer's task.

Jp
 
Reply With Quote
 
 
 
 
Noam Raphael
Guest
Posts: n/a
 
      12-23-2004
Jp Calderone wrote:
> This lets you avoid duplicate test code as well as easily test
> new concrete implementations. It's an ideal approach for frameworks
> which mandate application-level implementations of a particular
> interface and want to ease the application developer's task.
>
> Jp


It's a great way for sharing tests between different subclasses of a
class. Thank you for teaching me.

However, I'm not sure if this solves my practical problem - testing
whether all abstract methods were implemented. I think that usually, you
can't write a test which checks whether an abstract method did what it
should have, since different implementations do different things. I
don't even know how you can test whether an abstract method was
implemented - should you run it and see if it raises a
NotImplementedError? But with what arguments? And even if you find a way
to test whether a method was implemented, I still think that the
duplication of code isn't very nice - you have both in your class
definition and in your test suite a section which says only "method
so-and-so should be implemented."

I think that making abstract methods a different object really makes
sense - they are just something else. Functions (and methods) define
what the computer should do. Abstract methods define what the
*programmer* should do.

Again, thanks for enlightening me.
Noam
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
V1.1 Virtual Folder when V2.0 installed for the virtual server? Jéjé ASP .Net 2 11-30-2005 05:44 PM
virtual template and virtual access for ADSL circuits Gary Cisco 1 04-28-2005 07:26 PM
Virtual Computer Corporation (VCC) Virtual Workbench VW300 Derek Simmons VHDL 0 08-01-2004 04:55 AM
Deploying Servlets in virtual host / virtual filesystem environment while_1 Java 2 06-25-2004 06:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57