[pypy-dev] having broken the back of the unittest conversion problem ...

holger krekel hpk at trillke.net
Thu Jun 17 08:44:42 CEST 2004

Morning Laura, 

[Laura Creighton Thu, Jun 17, 2004 at 08:00:37AM +0200]
> I now need a bit more help.   I made this file.  
> Thanks, Laura
> """
> I've been looking at unittest.py, and I have a few questions.
> First  - is this conversion table ok as far as it goes?


> Second - do we ever use the msg argument, or always the None default?

i think we use it only in a few places if any. If i see it correctly
then honoring the msg would kill your/Armin's approach of looking for
the significant comma.  Or is there a simple solution?  

So i guess the approach should be to forget about it and see if anything
screws up. 

> Third  - what about the ones I left out?  what is the 'raises' syntax?

actually the same syntax as with assertRaises. So assertRaises 
should basically be a simple replace('assertRaises', 'raises'). 

> Fourth - do we ever use them without a preceeding self. ?

no, don't worry. this is hardly feasible. 

> Fifth  - what about the imports?  we need to convert them too.
>          import unittest  --> from std.utest import raises, whatelse ?

i think you don't need to change the imports because we usually
import 'pypy.tool.testit' not unittest directly.  We will have to do some
more stuff for testit.AppTestCases etc.pp. For simplicity of the
automatic conversion we can just continue to do it the old way
(as opposed to prefix all app-tests with app_test_, which can be
converted separately if needed). 

> #def assert_(self, expr, msg=None):
> self.assert_(expr)     --> assert expr
> self.failUnless(expr)  --> assert expr
> #def assertEqual(self, first, second, msg=None):
> self.assertEqual(left, right)      --> assert left == right
> self.assertEquals(left, right)     --> assert left == right
> self.failUnlessEqual(left, right)  --> assert left == right
> #def assertNotEqual(self, first, second, msg=None):
> self.assertNotEqual(left, right)  --> assert left != right
> self.assertNotEquals(left, right) --> assert left != right
> self.failIfEqual(left, right)     --> assert left != right

right.  (Isn't the number of aliases incredible?) 

> # I don't know the precise syntax for raises
> #def assertRaises(self, excClass, callableObj, *args, **kwargs):
> self.assertRaises(excClass, callableObj, *args, **kwargs)
>        --> raises(ExpectedException, 'now what?', 'now what2?')
             raises(excClass, callableObj, *args, **kwargs)

> self.failUnlessRaises(excClass, callableObj, *args, **kwargs)
>        --> raises(ExpectedException, 'now what?', 'now what2?')

             raises(excClass, callableObj, *args, **kwargs)

> #def fail(self, msg=None):

doesn't exist in utest but will have to be re-added because we apparently
use this in a few places.  Probably with 'from std.utest import fail'. 

> #def failIf(self, expr, msg=None):

    assert not expr

> #def failUnless(self, expr, msg=None):

    assert expr

> # And I am even more clueless about what you want me to do
> # about these friends.
> #def failUnlessAlmostEqual(self, first, second, places=7, msg=None):
> #assertAlmostEqual = assertAlmostEquals = failUnlessAlmostEqual 
> #def failIfAlmostEqual(self, first, second, places=7, msg=None):
> #assertNotAlmostEqual = assertNotAlmostEquals = failIfAlmostEqual

these are new in python2.3 IIRC and can be (if we use it at all) 
converted to: 

    assert round(first-second, 7) == 0 


    assert round(first-second, 7) != 0 




More information about the Pypy-dev mailing list