5

问题

作为 python unittest 的一部分,将加载一些输入 json 文件,这些文件存在于“data”目录下,该目录位于与测试 py 文件相同的目录中。

'pkg_resources' 用于此目的。

当 unittest 使用 python 运行时,它工作正常。但是在运行扭曲试验时它会失败。

我的项目混合了测试用例与 python unittest 测试用例以及 twisted.trial.unittest 测试用例。因此,通常需要使用扭曲试验运行两种类型的测试用例。

运行带有扭曲试验的测试用例时,会在路径中添加“_trial_temp”目录。请让我知道有什么办法可以解决这个问题吗?

示例目录结构:

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

试验测试.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()

使用 python 及其输出运行测试:

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

OK

使用 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)
4

2 回答 2

4

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 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.

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; or perhaps even 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. This means you should have a clear idea of what myproject/tests/test_trialtest.py's module name is supposed to be. Is myproject supposed to be on 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. 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; 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.

于 2015-03-28T00:16:43.447 回答
-1

__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)
于 2015-03-28T07:02:39.240 回答