Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45739 > unrolled thread

subclassing from unittest

Started byCharles Smith <cts.private.yahoo@gmail.com>
First post2013-05-22 08:32 -0700
Last post2013-05-23 15:42 -0400
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  subclassing from unittest Charles Smith <cts.private.yahoo@gmail.com> - 2013-05-22 08:32 -0700
    Re: subclassing from unittest Charles Smith <cts.private.yahoo@gmail.com> - 2013-05-22 08:47 -0700
    Re: subclassing from unittest Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-22 17:29 -0400
    Re: subclassing from unittest Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-05-23 08:58 +0200
      Re: subclassing from unittest Roy Smith <roy@panix.com> - 2013-05-23 08:43 -0400
      Re: subclassing from unittest Terry Jan Reedy <tjreedy@udel.edu> - 2013-05-23 15:42 -0400

#45739 — subclassing from unittest

FromCharles Smith <cts.private.yahoo@gmail.com>
Date2013-05-22 08:32 -0700
Subjectsubclassing from unittest
Message-ID<07786a18-bb9f-4434-823c-d0fe00f858b0@cl9g2000vbb.googlegroups.com>
Hi,

I'd like to subclass from unittest.TestCase.  I observed something
interesting and wonder if anyone can explain what's going on... some
subclasses create  null tests.

I can create this subclass and the test works:

  class StdTestCase (unittest.TestCase):
      blahblah

and I can create this subsubclass and the test works:

  class aaaTestCase (StdTestCase):
      moreblahblah

but if I create this subsubclass (or any where the first letter is
capital):

  class AaaTestCase (StdTestCase):
      differentblahblah

the test completes immediately without any work being done.

I suspect that the answer is in the prefix printed out by the test.  I
have diffed both the long output (tests works, on the left) and the
short output (null test, on the right):

test
(TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM_Tx) ...                  <
test suite has  <unittest.TestSuite tests=[<unittest.TestSuite
tests=[]>,       test suite has  <unittest.TestSuite
tests=[<unittest.TestSuite tests=[]>,
 
>    <unittest.TestSuite tests=[]>,
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM
<unittest.TestSuite tests=[<TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0001_ULLA10_xxxxxxxx_AM
<unittest.TestSuite tests=[<TC_02.TestCase_F_0001_ULLA10_xxxxxxxx_AM
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0002_ULLA20_xxxxxxxx_AM
<unittest.TestSuite tests=[<TC_02.TestCase_F_0002_ULLA20_xxxxxxxx_AM
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0003_ULLA05_xxxxxxxx_UM
<unittest.TestSuite tests=[<TC_02.TestCase_F_0003_ULLA05_xxxxxxxx_UM
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0005_ULLA10_xxxxxxxx_UM
<unittest.TestSuite tests=[<TC_02.TestCase_F_0005_ULLA10_xxxxxxxx_UM
 <unittest.TestSuite
tests=[<TC_02.TestCase_F_0006_ULLA20_xxxxxxxx_UM   |
<unittest.TestSuite tests=[<TC_02.TestCase_F_0006_ULLA20_xxxxxxxx_UM
 <unittest.TestSuite
tests=[]>]>                                        <
 
>   ---------->  test_api_socket:the address specified is:  127.0.0.1
 
>
 
>
 
>
 
>
----------------------------------------------------------------------
 
>   Ran 0 tests in 0.000s
 
>
 
>   OK


I see an empty test somehow gets sorted to the beginning of the list.
How could that be a result of whether the first letter of the class is
capitalized or not?

Thanks in advance...
cts

--------------------------------------------------------
http://www.creative-telcom-solutions.de

[toc] | [next] | [standalone]


#45740

FromCharles Smith <cts.private.yahoo@gmail.com>
Date2013-05-22 08:47 -0700
Message-ID<4adefe8b-d41c-488d-a695-53a62c497b91@q8g2000vbl.googlegroups.com>
In reply to#45739
On 22 Mai, 17:32, Charles Smith <cts.private.ya...@gmail.com> wrote:
> Hi,
>
> I'd like to subclass from unittest.TestCase.  I observed something
> interesting and wonder if anyone can explain what's going on... some
> subclasses create  null tests.
>
> I can create this subclass and the test works:
>
>   class StdTestCase (unittest.TestCase):
>       blahblah
>
> and I can create this subsubclass and the test works:
>
>   class aaaTestCase (StdTestCase):
>       moreblahblah
>
> but if I create this subsubclass (or any where the first letter is
> capital):
>
>   class AaaTestCase (StdTestCase):
>       differentblahblah
>
> the test completes immediately without any work being done.
>
> I suspect that the answer is in the prefix printed out by the test.  I
> have diffed both the long output (tests works, on the left) and the
> short output (null test, on the right):
>
> test
> (TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM_Tx) ...                  <
> test suite has  <unittest.TestSuite tests=[<unittest.TestSuite
> tests=[]>,       test suite has  <unittest.TestSuite
> tests=[<unittest.TestSuite tests=[]>,
>
> >    <unittest.TestSuite tests=[]>,
>
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0000_ULLA05_xxxxxxxx_AM
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0001_ULLA10_xxxxxxxx_AM
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0001_ULLA10_xxxxxxxx_AM
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0002_ULLA20_xxxxxxxx_AM
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0002_ULLA20_xxxxxxxx_AM
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0003_ULLA05_xxxxxxxx_UM
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0003_ULLA05_xxxxxxxx_UM
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0005_ULLA10_xxxxxxxx_UM
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0005_ULLA10_xxxxxxxx_UM
>  <unittest.TestSuite
> tests=[<TC_02.TestCase_F_0006_ULLA20_xxxxxxxx_UM   |
> <unittest.TestSuite tests=[<TC_02.TestCase_F_0006_ULLA20_xxxxxxxx_UM
>  <unittest.TestSuite
> tests=[]>]>                                        <
>
> >   ---------->  test_api_socket:the address specified is:  127.0.0.1
>
> ----------------------------------------------------------------------
>
> >   Ran 0 tests in 0.000s
>
> >   OK
>
> I see an empty test somehow gets sorted to the beginning of the list.
> How could that be a result of whether the first letter of the class is
> capitalized or not?
>
> Thanks in advance...
> cts
>
> --------------------------------------------------------http://www.creative-telcom-solutions.de


Unfortunately, the side-by-side diff didn't come out so well  ...

---------------------------------------
http://www.creative-telcom-solutions.de

[toc] | [prev] | [next] | [standalone]


#45752

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-05-22 17:29 -0400
Message-ID<mailman.1976.1369258172.3114.python-list@python.org>
In reply to#45739
On 5/22/2013 11:32 AM, Charles Smith wrote:

Have you red this? I will suggest some specifics.
http://www.catb.org/esr/faqs/smart-questions.html

> I'd like to subclass from unittest.TestCase.

What version of Python.

 > I observed something interesting and wonder if anyone can explain 
what's going on... some
> subclasses create  null tests.
>
> I can create this subclass and the test works:

What does 'works' mean?

>    class StdTestCase (unittest.TestCase):
>        blahblah

I bet that this (and the rest of your 'code' is not what you actually 
ran. Unless blahblah is bound (to what?), this fails with NameError.
Give us what you ran so we can run it too, and modify it.

> and I can create this subsubclass and the test works:
>
>    class aaaTestCase (StdTestCase):
>        moreblahblah
>
> but if I create this subsubclass (or any where the first letter is
> capital):
>
>    class AaaTestCase (StdTestCase):
>        differentblahblah
>
> the test completes immediately without any work being done.

What does this mean? I see no difference with the following

import unittest
class StdTestCase (unittest.TestCase): pass
class lowerSub(StdTestCase): pass
class UpperSub(StdTestCase): pass

unittest.main(verbosity=2, exit=False)

# prints (3.3)
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Same as before the subclasses were added.

--
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#45780

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-05-23 08:58 +0200
Message-ID<bar07a-pca.ln1@satorlaser.homedns.org>
In reply to#45739
Am 22.05.2013 17:32, schrieb Charles Smith:
> I'd like to subclass from unittest.TestCase.  I observed something
> interesting and wonder if anyone can explain what's going on... some
> subclasses create  null tests.

I can perhaps guess what's going on, though Terry is right: Your 
question isn't very helpful and informative.


> I can create this subclass and the test works:
>
>    class StdTestCase (unittest.TestCase):
>        blahblah
>
> and I can create this subsubclass and the test works:
>
>    class aaaTestCase (StdTestCase):
>        moreblahblah
>
> but if I create this subsubclass (or any where the first letter is
> capital):
>
>    class AaaTestCase (StdTestCase):
>        differentblahblah
>
> the test completes immediately without any work being done.

Well, per PEP 8, classes use CamelCaps, so your naming might break 
automatic test discovery. Then, there might be another thing that could 
cause this, and that is that if you have an intermediate class derived 
from unittest.TestCase, that class on its own will be considered as test 
case! If this is not what you want but you still want common 
functionality in a baseclass, create a mixin and then derive from both 
the mixin and unittest.TestCase for the actual test cases.

Good luck!

Uli

[toc] | [prev] | [next] | [standalone]


#45807

FromRoy Smith <roy@panix.com>
Date2013-05-23 08:43 -0400
Message-ID<roy-63E1C3.08435923052013@news.panix.com>
In reply to#45780
In article <bar07a-pca.ln1@satorlaser.homedns.org>,
 Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> wrote:

> if you have an intermediate class derived 
> from unittest.TestCase, that class on its own will be considered as test 
> case! If this is not what you want but you still want common 
> functionality in a baseclass, create a mixin and then derive from both 
> the mixin and unittest.TestCase for the actual test cases.

Or, try another trick I picked up somewhere.  When you're done defining 
your test classes, delete the intermediate base class, so it won't be 
autodiscovered!

class MyBaseTestClass(unittest.TestCase):
   pass

class MyRealTest1(MyBaseTestClass):
   pass

class MyRealTest2(MyBaseTestCalss):
   pass

del MyBaseTestClass

[toc] | [prev] | [next] | [standalone]


#45836

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-05-23 15:42 -0400
Message-ID<mailman.2031.1369338168.3114.python-list@python.org>
In reply to#45780
On 5/23/2013 2:58 AM, Ulrich Eckhardt wrote:

> Well, per PEP 8, classes use CamelCaps, so your naming might break
> automatic test discovery. Then, there might be another thing that could
> cause this, and that is that if you have an intermediate class derived
> from unittest.TestCase, that class on its own will be considered as test
> case! If this is not what you want but you still want common
> functionality in a baseclass, create a mixin and then derive from both
> the mixin and unittest.TestCase for the actual test cases.

This is now standard practice, gradually being implemented everywhere in 
the CPython test suite, for testing C and Py versions of a module.

class TestXyz():
   mod = None
   <test_a, etc, methods>

class TestXyz_C(TestXyz, TextCase):  # Test C version
   mod = support.import_fresh_module('_xyz')  # approximately right

class TestXyz_Py(TestXyz, TextCase):  # Test Python version
   mod = support.import_fresh('xyz')

This minimizes duplication and ensures that both implementations get 
exactly the same tests.

tjr

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web