How to check if code from nose test is running?

I have code that is used in unit test. However, a downloadable library requires some data that is not really required for a nose test, because that data is ridiculed by unit test. I would like to protect the reading of files in the library so that they are not called in case of a nose test.

Is there an easy way to do this?

Maybe I can do something with sys.modules or the original command line, but I would prefer something more elegant if it exists.

+5
source share
2 answers

As mentioned in the comments, the structure of this code is a mess, and part of the point of the tests is to make sure that I don't break things when I reorganize ...

So at the moment (if someone does not give me a better answer), I use:

if 'nose' not in sys.modules.keys(): <read the data> 
+6
source

The correct approach would be to mock all the code with side effects (I will assume that you don't want to) with empty mocks.

This file is test_module my_module:

 def do_something_and_destroy_world(): destroy_world() return None 

Sample test file:

 import mock import unittest import my_module class MyTest(unittest.TestCase): def testSomethingUgly(self): with mock.patch('my_module.destroy_world', return_value=None): result = do_something_and_destroy_world() self.assertIsNone(result) 

When the tests are executed, the statement will be correct, and destroy_world will not be called - instead, it will be replaced by an empty layout with a fixed return value.

+2
source

Source: https://habr.com/ru/post/1239830/


All Articles