[py-dev] Failing to manage test state of a module with py.test

Maciek Fijalkowski fijal at genesilico.pl
Thu Nov 16 22:34:41 CET 2006


Pierre Rouleau wrote:

> On 11/16/06, Maciek Fijalkowski <fijal at genesilico.pl> wrote:
>
>> Pierre Rouleau wrote:
>>
>> >Hi all,
>> >
>> >Using the latest of py.test (svn 34683), I can't seem to be able to
>> >control a module test setup using setup_module() as described in
>> >section 2.15 of http://codespeak.net/py/current/doc/test.html
>> >
>> >The document gives the following example:
>> >
>> >def setup_module(module):
>> >    """ setup up any state specific to the execution
>> >        of the given module.
>> >    """
>> >
>> >Now, I am assuming that the the definition of setup_module() must be
>> >written /inside/ the test script that test the module-under-test
>> >tested, right?
>> >
>> >Second, I am also assuming that setup_module's argument is the name of
>> >the module-under-test.
>> >
>> >Given the above assumptions, if I write a test script to test roman.py
>> > would be called test_roman.py and would include
>> >
>> >def setup_module(roman):
>> >    print 'SETTING up roman for testing....'
>> >
>> >And if I ran py.test -s test_roman.py I should be able to see the
>> >printed output. I don't.
>> >
>> >So, what do i do wrong here?
>> >
>> >Thanks
>> >_______________________________________________
>> >py-dev mailing list
>> >py-dev at codespeak.net
>> >http://codespeak.net/mailman/listinfo/py-dev
>> >
>> >
>> Hum. I cannot reproduce you problem (I can clearly see 'SETTING up
>> roman...' just after [5024] and before any dots. Yes, this is supposed
>> to go to main body of testing module and it receives module object
>> itself (not a name)
>>
>
> Sorry, i also got it working if all the setup_module() does is
> printing the message.
>
> I forgot to mention that the setup_module() was setting the value of
> knownValues after declaring it global.  I though that it would then be
> available to all test functions (as is the case with nose), but it's
> not.
>
> The setup_module I have looks like this:
>
> def setup_module(roman):
>    print 'SETTING UP........................'
>
>    global knownValues
>    knownValues = ( (1, 'I'),
>                    (2, 'II'),
>                    (3, 'III'),
>                    (4, 'IV'),
>                    (5, 'V'),
>                    (6, 'VI'),
>                    (7, 'VII'),
>                    ....
>
>
> I wanted to know if the values set up by the setup_module function
> would be available to the test functions.  The test function that
> fails is failing because knownValues is not accessible to the check
> inside test_known_values.
>
> def test_known_values() :
>    """Test all known values."""
>
>    def check(number, roman_numeral):
>        print 'test_known_values Testing: ', roman_numeral
>        assert roman.toRoman(number) == roman_numeral
>        assert roman.fromRoman(roman_numeral) == number
>        # all Roman numerals should be in upper case
>        assert roman_numeral.upper() == roman_numeral
>        # lower case Roman numerals are not accepted
>        raises(roman.InvalidRomanNumeralError, roman.fromRoman,
> roman_numeral.lower())
>
>    tested_numbers = []
>
>    # test the numbers in the table above
>    for number, roman_numeral in knownValues:
>        yield check, number, roman_numeral
>        tested_numbers.append(number)
>
>    # test the others
>    for number in xrange(1,5000):
>        if number not in tested_numbers:
>            yield check, number, roman.toRoman(number)
>
>
>
> Note that nose is able to recover the global variable and the test
> succeeds in nosetests. py.test does not seem able to do it.  It's not
> that I absolutely want to use global variables, but I wanted to checks
> the module setup mechanism.
>
> -- 
>
> P.R.

Of course tha you just write:

def setup_module(mod):
    mod.some_global_value = whatever_you_like

:-) It's of course better, because explicit is better than implicit (you 
avoid exporting all variables used in setup_)



More information about the Pytest-dev mailing list