2

I am currently writing tests for a bit of Python (using the 3.5 version) software for the company I'm working with, but am stumbling upon a problem which I currently believe is because of global variables if I could call it that.

The software architecture is extremely modular, hence to test individual modules a separate test file (perhaps called test_somemodule_py) is created containing one class that extends the unittest.TestCase one.

NOTE that what I call modules here is not really a Python module, but rather an application module

All of the tests are triggered through a single runner script which contains a custom loader:

def load_tests(loader, tests, pattern):
suite = unittest.TestSuite()
for all_tests in loader.discover('tests', pattern='test_*.py'):
    for test_suite in all_tests:
        suite.addTests(test_suite)

return suite

During actual runtime all of the individual modules are held (contained) in a master like wrapper, which is basically a Singleton.

Now my problem is that whenever I run the tests as individual files (changing the loader from test_*.py to test_somemodule.py), all tests succeed, however if they are run all together (leaving the test_*.py pattern), some of the tests start failing.

I am pretty sure it was never the case when I hadn't implemented the master wrapper as a singleton, but the application functionality and architecture really requires that kind of approach. Since I am suspecting that only one instance of it is created, can anyone advise me on how to best solve this issue?

My theory is that if there was a method to reset all 'globals' to an initial state, it would work like a charm. Or perhaps there is some sort of method to isolate memory which is then discarded when test goes out of scope.

Many thanks in advance.

6
  • I may be way off here, but are you using the setup and teardown methods in your tests? Commented Jan 25, 2018 at 14:46
  • 1
    I am. However I am not really utilising them for the case I described in my question. The big issue is that I'm not sure if what I'm trying to achieve is at all possible and if it is - I am not sure how. Documentation is plenty, but looking for the answer is a bit like searching for a needle in a Pystack. Commented Jan 25, 2018 at 14:50
  • maybe try using PyTest, put all of your tests in one module called test_them_all.py or somting like that, that way each test is it's own scope. Commented Jan 25, 2018 at 15:10
  • for more info : stackoverflow.com/questions/27954702/unittest-vs-pytest Commented Jan 25, 2018 at 15:12
  • depending on strcuture of ur code, choose setUp, setUpClass, setUpModule or fixture, detail [nose] (nose.readthedocs.io/en/latest/writing_tests.html#test-classes) Commented Jan 25, 2018 at 23:25

1 Answer 1

1

Why not do this?

for name in dir():
    if not name.startswith('_'):
        del globals()[name]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.