Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


How to use Run As > Python Unit Test

  • Xsaero

    I can't find any docs on how to use Pyhton Unit Test  in pyhton file context menues. Ehen I try it it simply gives tels me that 0 tests have been succesfully ran even thought there are TestCase classes in that file. If I just run the files the normal way using unittest.main() they execute just fine. Is there some convention that I need to follow to get that working.

    • Xsaero

      Additional Info: I use PyDev 1.2.1 with Extensions 1.2.1

      • Fabio Zadrozny
        Fabio Zadrozny

        Well, it seems like a misconfiguration...

        If your pythonpath is not correctly configured it might not be able to run your unit-test (so, maybe your project source folder is not correctly configured?)

        Have you checked: http://fabioz.com/pydev/manual_101_root.html



    • Xsaero

      I have the pyhton path configured correctly. The source forlder in in the PyhtonPath.

    • drmaples

      can you post the code you are trying to run as "unit test". 

      • Xsaero

        Unit Test Code:

        import unittest

        from Packages.Utilities.validate import *

        class validateTest( unittest.TestCase ):
           """ Class to test validation functions """
           def testIsLargeID(self):
              """ Test isLargeID function """
              self.assert_( isLargeID( 1 ) )
              self.assert_( isLargeID( '1' ) )
              self.assert_( isLargeID( u'1' ) )
              self.assert_( isLargeID( r'1' ) )
              self.assert_( isLargeID( 0 ) )
              self.assert_( isLargeID( '0' ) )
              self.assert_( isLargeID( 00000000 ) )
              self.assert_( isLargeID( '0000000' ) )
              self.assert_( isLargeID( MAX_LARGE_ID ) )
              self.assert_( isLargeID( MAX_SMALL_ID ) )
              self.assert_( isLargeID( MIN_LARGE_ID ) )
              self.assert_( not isLargeID( -1 ) )
              self.assert_( not isLargeID( '-1' ) )
              self.assert_( not isLargeID( u'-1' ) )
              self.assert_( not isLargeID( None ) )
              self.assert_( not isLargeID( '' ) )
              self.assert_( not isLargeID( MAX_LARGE_ID + 1) )
              self.assert_( not isLargeID( MIN_LARGE_ID - 1 ) )
              self.assert_( not isLargeID( '0.0' ) )
              self.assert_( not isLargeID( float( MAX_LARGE_ID ) ) )
           def testIsSmallID(self):
              """ Test isSmallID function """
              self.assert_( isSmallID( 1 ) )
              self.assert_( isSmallID( '1' ) )
              self.assert_( isSmallID( u'1' ) )
              self.assert_( isSmallID( r'1' ) )
              self.assert_( isSmallID( 0 ) )
              self.assert_( isSmallID( '0' ) )
              self.assert_( isSmallID( 00000000 ) )
              self.assert_( isSmallID( '0000000' ) )
              self.assert_( isSmallID( MAX_SMALL_ID ) )
              self.assert_( isSmallID( MIN_SMALL_ID ) )
              self.assert_( not isSmallID( -1 ) )
              self.assert_( not isSmallID( '-1' ) )
              self.assert_( not isSmallID( u'-1' ) )
              self.assert_( not isSmallID( r'-1' ) )
              self.assert_( not isSmallID( None ) )
              self.assert_( not isSmallID( '' ) )
              self.assert_( not isSmallID( '   ' ) )
              self.assert_( not isSmallID( 'as' ) )
              self.assert_( not isSmallID( '1a' ) )
              self.assert_( not isSmallID( MAX_SMALL_ID + 1) )
              self.assert_( not isSmallID( MIN_SMALL_ID - 1 ) )
              self.assert_( not isSmallID( '0.0' ) )
              self.assert_( not isSmallID( float( MAX_SMALL_ID ) ) )     
        def TestSuite():
           return unittest.makeSuite(validateTest)

        if __name__ == '__main__':

        And here are the validation functions:

        def isLargeID( id ):
           """ Function to tell whether the id is a large id """
           return isInteg( id ) and withinRange( id , MIN_LARGE_ID, MAX_LARGE_ID )

        def isSmallID( id ):
           """ Function to tell whether the id is a small id """
           return isInteg( id ) and withinRange( id , MIN_SMALL_ID, MAX_SMALL_ID )

        def withinRange( value, min, max ):
           """ Function to check the range """
           if isNumeric( value )  and isNumeric( min ) and isNumeric( max ):
              value = float( value )
              min = float( min )
              max = float( max )
              return min <= max and value >= min and value <= max

        • Fabio Zadrozny
          Fabio Zadrozny

          Well, really strange, it should work... As it is difficult for me to reproduce it here, do you think you could try debugging the script yourself?

          It should be straightforward:

          Pydev has a script that is called for running the tests:

          But now that I'm writing it, I just reminded that your problem might be the pydev version... earlier versions of pydev had a bug in that the unit-test would only run if you selected a folder (the problem was in that script), and not a .py file (this is already fixed, but as you didn't say your pydev version, this might be your problem).



        • drmaples

          code seems ok, but try this:

          pydev 1.2.1 and below has a bug where some errors are silently ignored and 0 tests are reported as running. check that you do not have any syntax errors in the code that you are importing. the best thing to do is to change the code in runfiles.py to look like the code below. (this is fixed in the next ver of pydev) 

          code located here:

                      module = ImportModule(ModuleName(name, system_path))
                      tests = loader.loadTestsFromModule(module)
                  except Exception, msg:
                      raise   # <------ add me this
                     #ok, unable to load the module (probably has not __init__.py in its folder structure)

          • Xsaero

            Thanks. That helped. I had no idea that the sctipt that runs test first imports them as modules. In my Test folders I did not have __init__.py file because  I had no reason for importing tests.

            I think somewhere in documentation it should mention that test folders have to be Python packages to be run as Unit Tests from context menu.

            • Fabio Zadrozny
              Fabio Zadrozny

              I think the test-runner could give a warning about it if it found no tests... what do you think? (if you agree, please open a feature request for it).