Re-running unittest

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jul 23 04:24:28 CEST 2007


En Sun, 22 Jul 2007 17:43:03 -0300, Israel Fernández Cabrera  
<iferca at gmail.com> escribió:

> I'm writing some code that automatically execute some registered unit
> test in a way to automate the process. A sample code follows to
> illustrate what I'm doing:
>
> <code requires="save as import_tests.py">
> class PruebasDePrueba(unittest.TestCase):
>    def testUnTest(self):
>        a = 2
>        b = 1
>        self.assertEquals(a, b)
>
> def runTests():
>    loader = unittest.TestLoader()
>    result = unittest.TestResult()
>    suite = loader.loadTestsFromName("import_tests.PruebasDePrueba")
>    suite.run(result)
>    print "Errores: ", len(result.errors)
>    print "Fallos: ", len(result.failures)
>
> if __name__ == "__main__":
>    runTests()
>    raw_input("Modify [fix] the test and press ENTER to continue")
> </code>
>
> The code executes the tests from the class PruebasDePrueba, as the
> user to "fix" the failing test and then executes the tests again after
> ENTER is pressed.
> The test's initial state is "fail" so, equaling the values of a or b
> in the second execution I wait the test does not fails again, but it
> does.
> I've changed the original code in very different ways trying to figure
> out what is wrong with it but no success
> The problem is not reproduced if instead of loading the test from the
> TestCase (import_tests.PruebasDePrueba) they are loaded referring the
> container module and this behaves like this because I wrote a class
> that inherits from unittest.TestLoader abd re-defines the
> loadTestsFromModule(module) then every time this method is called, the
> module is reloaded via "reload" python's function. I would like to do
> the same with TestCases.

I'm a bit confused. Perhaps the description above does not match the code.  
The code does not use reload, and has no loops, so it is executed only  
once. The way I interpret it: I run the script, the test fails; I "fix"  
testUnTest; I run the script again, it passes.
When I say "I run the script", I mean typing "python import_tests.py" at  
the console, or similar.
Perhaps you are running it from inside another environment, like IDLE, and  
you keep objects created which won't notice the changed code, even if you  
use reload(). The answer would be: don't do that. Read the last comments  
in the reload documentation  
<http://docs.python.org/lib/built-in-funcs.html#l2h-61>

Another thing I don't understand, is that usually one fixes the CODE until  
it passes the tests; fixing the TESTS so the code passes looks a bit  
strange.

> I have written this problem to several other python lists but I have
> not received any answer, hope this time is different,
> I'd like to thaks in advance, regards

Perhaps you have to explain it further. If your problem is the usage of  
reload, as I think, this has nothing to do with unit tests, and worse,  
your code does not even show how you use that function.

-- 
Gabriel Genellina




More information about the Python-list mailing list