Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unit testing errors (testing the platform module)

3 views
Skip to first unread message

John Maclean

unread,
Apr 13, 2010, 10:01:19 AM4/13/10
to pytho...@python.org
I normally use languages unit testing framework to get a better
understanding of how a language works. Right now I want to grok the
platform module;


1 #!/usr/bin/env python
2 '''a pythonic factor'''
3 import unittest
4 import platform
5
6 class TestPyfactorTestCase(unittest.TestCase):
7 def setUp(self):
8 '''setting up stuff'''
13
14 def testplatformbuiltins(self): 15
'''platform.__builtins__.blah '''
16 self.assertEquals(platform.__builtins__.__class__, "<type 'd
ict'>")
17
18
19 def tearDown(self):
20 print 'cleaning stuff up'
21
22 if __name__ == "__main__":
23 unittest.main()


Is there an error in my syntax? Why is my test failing? Line 16.


python stfu/testing/test_pyfactor.py
Fcleaning stuff up

======================================================================
FAIL: platform.__builtins__.blah
----------------------------------------------------------------------
Traceback (most recent call last):
File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")
AssertionError: <type 'dict'> != "<type 'dict'>"

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

--
John Maclean MSc. (DIC) Bsc. (Hons),Core Linux Systems Engineering,07739
171 531

Martin P. Hellwig

unread,
Apr 13, 2010, 10:11:29 AM4/13/10
to

What happens if you change this line:
self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")

To something like:
self.assertEquals(platform.__builtins__.__class__, type(dict()))

or
self.assertEquals(str(platform.__builtins__.__class__), "<type 'dict'>")

--
mph

MRAB

unread,
Apr 13, 2010, 10:19:14 AM4/13/10
to pytho...@python.org
platform.__builtins__.__class__ returns a dict, which is not the same as
"<type 'dict'>", a string.

J. Cliff Dyer

unread,
Apr 13, 2010, 10:28:28 AM4/13/10
to John Maclean, pytho...@python.org
The problem is that the class of platform.__builtins__ is a dict, not a
string containing the text "<type 'dict'>".

Try replacing line 16 with this:

self.assertEqual(type(platform.__builtins__), dict)

Cheers,
Cliff

Gabriel Genellina

unread,
Apr 14, 2010, 4:09:54 AM4/14/10
to pytho...@python.org
En Tue, 13 Apr 2010 11:01:19 -0300, John Maclean <jay...@gmail.com>
escribi�:

> Is there an error in my syntax? Why is my test failing? Line 16.
>

> ======================================================================
> FAIL: platform.__builtins__.blah
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "stfu/testing/test_pyfactor.py", line 16, in testplatformbuiltins
> self.assertEquals(platform.__builtins__.__class__, "<type 'dict'>")
> AssertionError: <type 'dict'> != "<type 'dict'>"
>
> ----------------------------------------------------------------------

To express the condition "SOMEOBJECT must be a dictionary", use:

isinstance(SOMEOBJECT, dict)

SOMEOBJECT might actually be an instance of any derived class and still
pass the test; that's usually the desired behavior.

In the rare cases where only a very specific type is allowed, use this
form instead:

type(SOMEOBJECT) is dict


The test case above should read then:

self.assert_(isinstance(platform.__builtins__, dict),
type(platform.__builtins__))

--
Gabriel Genellina

john maclean

unread,
Apr 14, 2010, 10:51:55 AM4/14/10
to pytho...@python.org
On 14 April 2010 09:09, Gabriel Genellina <gags...@yahoo.com.ar> wrote:
> En Tue, 13 Apr 2010 11:01:19 -0300, John Maclean <jay...@gmail.com>
> escribió:
> --
> http://mail.python.org/mailman/listinfo/python-list
>

This is cool. Thanks for your replies.

self.assertEqual(platform.__builtins__.__class__, dict,
"platform.__class__ supposed to be dict")
self.assertEqual(platform.__name__, 'platform' )


--
John Maclean
07739 171 531
MSc (DIC)

Enterprise Linux Systems Engineer

J. Cliff Dyer

unread,
Apr 14, 2010, 11:19:47 AM4/14/10
to john maclean, pytho...@python.org
On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
> self.assertEqual(platform.__builtins__.__class__, dict,
> "platform.__class__ supposed to be dict")
> self.assertEqual(platform.__name__, 'platform' )

The preferred spelling for:

platform.__builtins__.__class__

would be

type(platform.__builtins__)

It's shorter and easier to read, but essentially says the same thing.

You can also use it on integer literals, which you can't do with your
syntax:

>>> type(1)
<type 'int'>
>>> 1.__class__
...
SyntaxError: invalid syntax

Admittedly, this is a trivial benefit. If you're using a literal, you
already know what type you're dealing with.

Cheers,
Cliff

Terry Reedy

unread,
Apr 14, 2010, 12:31:20 PM4/14/10
to pytho...@python.org
On 4/14/2010 11:19 AM, J. Cliff Dyer wrote:
> On Wed, 2010-04-14 at 15:51 +0100, john maclean wrote:
>> self.assertEqual(platform.__builtins__.__class__, dict,
>> "platform.__class__ supposed to be dict")
>> self.assertEqual(platform.__name__, 'platform' )
>
> The preferred spelling for:
>
> platform.__builtins__.__class__
>
> would be
>
> type(platform.__builtins__)

Agreed

> It's shorter and easier to read, but essentially says the same thing.
>
> You can also use it on integer literals, which you can't do with your
> syntax:
>
> >>> type(1)
> <type 'int'>
> >>> 1.__class__
> ...
> SyntaxError: invalid syntax

Add the needed space and it works fine.

>>> 1 .__class__
<class 'int'>

A possible use of literal int attributes is for bound mehods:

>>> inc = 1 .__add__
>>> inc(3)
4
>>> inc(3.0)
NotImplemented

Whereas def inc(n): return n+1 is generic and would return 4.0.

Terry Jan Reedy

0 new messages