简体   繁体   English

如何在Twisted试用测试中访问源树?

[英]how to access source tree in Twisted trial tests?

In my trial test case, I want to run scripts from my source tree. 在我的测试用例中,我想从源代码树中运行脚本。 Trial changes the working directory, so simple relative paths don't work. 试用会更改工作目录,因此简单的相对路径不起作用。 In practice, Trial's temporary directory is inside the source tree, but assuming that to be the case seems suboptimal. 实际上,Trial的临时目录位于源代码树中,但是假设情况并非如此。 Ie, I could do: 即,我可以这样做:

def source_file(p):
    return os.path.join('..', p)

Is there a better way? 有没有更好的办法?

If you want to find a file next to your test and run it as a script, you can just do this: 如果您想在测试旁边找到一个文件并将其作为脚本运行,则可以执行以下操作:

from twisted.python.modules import getModule
script = getModule(__name__).filePath.path
# ...
reactor.spawnProcess(..., script, ...)

You can also use this to support storing your code in a zip file, although invoking it with Python becomes a little more difficult that way. 您也可以使用它来支持将代码存储在zip文件中,尽管用Python调用它会变得更加困难。 Have you considered just using python -m ? 您是否考虑过仅使用python -m

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

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