简体   繁体   English

如何使用扭曲试验运行python unittest时加载资源文件

[英]how to load resource file while running python unittest using twisted trial

Problem 问题

As part of python unittest, some input json files are to be loaded which exists under 'data' directory which resides in the same directory of test py file. 作为python unittest的一部分,将加载一些输入json文件,这些文件存在于'data'目录下,该目录位于测试py文件的同一目录中。

'pkg_resources' is used for this purpose. 'pkg_resources'用于此目的。

It works fine when the unittest are running with python. 当unittest与python一起运行时,它工作正常。 But it fails when running with twisted trial. 但是在使用扭曲试验运行时失败了。

My project has mixed testcases with both python unittest testcases as well as twisted.trial.unittest testcases. 我的项目将testcase与python unittest测试用例以及twisted.trial.unittest测试用例混合在一起。 so, there is a need to run both type of testcases with twisted trial in general. 因此,一般需要使用扭曲试验来运行两种类型的测试用例。

The '_trial_temp' directory is added in path when running testcases with twisted trial. 使用扭曲试验运行测试用例时,会在路径中添加'_trial_temp'目录。 please, let me know there is any way to handle this? 拜托,让我知道有办法解决这个问题吗?

Example directory structure: 示例目录结构:

myproject/
└── tests
    ├── data
    │   └── input.json
    ├── trialTest.py

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 test using python and its output: 使用python及其输出运行测试:

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

OK

Running test using python and its output: 使用python及其输出运行测试:

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)

In the first example, __name__ will be set to __main__ , and the tests directory will be automatically added to sys.path . 在第一示例中, __name__将被设置为__main__ ,并且tests目录将被自动添加到sys.path This works more or less by accident; 这或多或少是偶然的; if you move your unittest.main invocation to another module, you won't be able to import it quite the same way, and data may not show up. 如果将unittest.main调用移动到另一个模块,则无法以相同的方式导入它,并且data可能不会显示。

In the second example, trial will, depending on the presence of an __init__.py file in the tests directory, set __name__ to either trialTest or tests.trialTest ; 在第二个示例中, trial将根据tests目录中__init__.py文件的存在,将__name__设置为trialTesttests.trialTest ; or perhaps even myproject.tests.trialTest . 或者甚至是myproject.tests.trialTest

You should re-name the module to test_trialtest.py so that it is discovered by trial's module-walking code properly, and then invoke it with a module name rather than a file name. 您应该将模块重命名为test_trialtest.py以便正确地通过试用模块行走代码发现它,然后使用模块名称而不是文件名来调用它。 This means you should have a clear idea of what myproject/tests/test_trialtest.py 's module name is supposed to be. 这意味着您应该清楚地了解myproject/tests/test_trialtest.py的模块名称应该是什么。 Is myproject supposed to be on sys.path ? myproject应该在sys.path吗? The parent directory? 父目录?

Basically, pkg_resources depends intimately on the details of the namespaces into which code is loaded and executed, so you need to be careful that everything is set up consistently. 基本上, pkg_resources依赖于加载和执行代码的命名空间的细节,因此您需要注意一切都是一致的。 If you make sure that everything is imported the same way, under the same name (never as __main__ , for example) then this should be totally consistent between trial and stdlib unittest ; 如果确保所有内容都以相同的方式导入,使用相同的名称(例如,从不__main__ ),那么在trial和stdlib unittest之间应该完全一致; there's nothing really special about trial here except that you are running it ( trial itself)` as the main script rather than your test script as the main script. 除了你正在运行它( trial本身)`作为主脚本而不是你的测试脚本作为主脚本之外,这里没有什么特别的试用。

place an __init__.py in tests directory resolves the issue. 在tests目录中放置__init__.py可以解决问题。

[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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM