[py-dev] Failing to manage test state of a module with py.test
holger at merlinux.de
Mon Jan 15 08:19:32 CET 2007
On Thu, Nov 16, 2006 at 16:22 -0500, Pierre Rouleau wrote:
> 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,
> tested_numbers = 
> # test the numbers in the table above
> for number, roman_numeral in knownValues:
> yield check, number, roman_numeral
> # 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.
So last week i tweaked py.test (rev 36386) to invoke
setup/teardown procedures before calling a "test generator".
Generative tests are a bit of an edge case in that the
generator already gets invoked during the collection process.
The collection process (in contrast to executing the tests)
usually does not invoke the setup/teardown logic but
i think it makes sense to regard test generators enough
of a special case.
(the other issue regarding working with the 'mod' argument
properly within setup_module(mod) has been clarified in
another subthread of your posting).
best & thanks for reporting!
More information about the Pytest-dev