How to load resource file when starting python unittest using twisted trial

Problem

As part of the python unittest, you need to load some input json files that exist in the "data" directory, which is located in the same folder of the py test file.

'pkg_resources' is used for this purpose.

It works great when unittest works with python. But it does not work at startup using a twisted process.

My project has mixed test tables with python unittest test packages, as well as twisted.trial.unittest test tests. therefore, there is a need to run both types of test boxes with twisted cork in general.

The directory '_trial_temp' is added to the path when running test files using a twisted trial version. please let me know if there is a way to handle this?

Example directory structure:

myproject/ └── tests β”œβ”€β”€ data β”‚ └── input.json β”œβ”€β”€ trialTest.py 

trialTest.py

 import unittest import inspect import pkg_resources class Test(unittest.TestCase): def test_01_pathTest(self): dataDirExists = pkg_resources.resource_exists(inspect.getmodule(self).__name__, 'data') print 'data exists: %s' % (dataDirExists) if __name__ == '__main__': unittest.main() 

Running a test using python and outputting it:

 cd myproject python tests/trialTest.py data exists: True . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK 

Running a test using python and outputting it:

 cd myproject /usr/local/bin/trial tests/trialTest.py trialTest Test test_01_pathTest ... data exists: False [OK] ------------------------------------------------------------------------------- Ran 1 tests in 0.013s PASSED (successes=1) 
+5
source share
2 answers

In the first example, __name__ will be set to __main__ , and the tests directory will be automatically added to sys.path . This works more or less by accident; if you move the unittest.main call to another module, you cannot import it in exactly the same way, and data may not be displayed.

In the second sample trial , depending on the presence of the __init__.py file in the tests directory, set __name__ to trialTest or tests.trialTest ; or maybe even myproject.tests.trialTest .

You must rename the module to test_trialtest.py so that it is correctly detected by the trial module, and then called with the module name, not the file name. This means that you should have a clear idea of ​​what the module name is myproject/tests/test_trialtest.py . Is myproject supposed to be on sys.path ? Parent directory?

Basically, pkg_resources is closely dependent on the details of the namespaces into which the code is loaded and loaded, so you need to be careful that everything is set up sequentially. If you make sure everything is imported the same way, under the same name (for example, not like __main__ ), this should be fully consistent between trial and stdlib unittest ; there is nothing special in the trial version, except that you run it ( trial yourself) as the main script, and not your test script as the main script.

+4
source

put the __init__.py directory in the test files, fixing the problem.

 [ durai@localhost myproject]$ touch tests/__init__.py [ durai@localhost myproject]$ tree . β”œβ”€β”€ tests  β”œβ”€β”€ data  β”‚  └── input.json  β”œβ”€β”€ __init__.py  β”œβ”€β”€ trialTest.py  └── trialTest.pyc [ durai@localhost myproject]$ trial tests/trialTest.py tests.trialTest Test test_01_pathTest ... currModule: <module 'tests.trialTest' from '/home/durai/Worskspace/myproject/tests/trialTest.pyc'> currModule: tests.trialTest data exists: True [OK] ------------------------------------------------------------------------------ - Ran 1 tests in 0.016s PASSED (successes=1) 
-1
source

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


All Articles