如何在pytest中导入函数 [英] how to import function in pytest
问题描述
我正在尝试为名称中包含破折号的 Python 2.x 脚本可执行文件创建一个 pytest
.我试图以通常的方式导入它,但我不知道如何让它与破折号一起工作.
I am trying to create a pytest
for a Python 2.x script executable with dashes included in its name. I tried to import it the usual way but I can't figure out how to make it work with the dashes.
我的项目结构如下:
package
-- tests
-- bin
-- subpackage
-- ...py
具体来说,我需要测试一个名为 master_disaster()
的函数,它存在于 bin/let-me-out
中(是的,-
).let-me-out
是一个可执行的 .py
文件,我的文件夹没有 setup.py
文件或任何类似的文件.
Specifically, I need to test a function called master_disaster()
which exists inside bin/let-me-out
(yes with -
). let-me-out
is an executable .py
file and my folder has no setup.py
file or anything similar.
如何在我的测试中导入这个函数?我的测试将是一个简单的装置,用于检查时间:
How can I import this function inside my test? My test is going to be a simple fixture that checks the time with:
@pytest.fixture
def now():
return timezone.now()
然后使用 now()
函数创建一个新文件,let-me-out
将在特定时间后删除该文件.
It then uses the now()
function to create a new file which let-me-out
will delete after a specific amount of time.
推荐答案
首先,破折号使 let-me-out
字成为 Python 中的无效标识符.要解决它,您必须调用 imp
(Python 2.7)或 importlib
(Python 3.5+) 机器.
First of all, dashes make let-me-out
word to an invalid identifier in Python. To work around it, you have to invoke the imp
(Python 2.7)
or importlib
(Python 3.5+) machinery.
以下是导入具有限定名称 let_me_out
、但使用 bin/let-me-out
作为源文件的新模块的示例:
Here is an example of importing a new module having a qualified name let_me_out
, but using bin/let-me-out
as source file:
import importlib
def test_master_disaster():
loader = importlib.machinery.SourceFileLoader('let_me_out', 'bin/let-me-out')
spec = importlib.util.spec_from_loader(loader.name, loader)
let_me_out = importlib.util.module_from_spec(spec)
loader.exec_module(let_me_out)
# this is only a stub, to show an example of calling the master_disaster function
assert let_me_out.master_disaster() == 'spam'
您可以将此代码提取到夹具中以使其可重用:
You can extract this code into a fixture to make it reusable:
import importlib
import pytest
@pytest.fixture(scope='session')
def let_me_out():
loader = importlib.machinery.SourceFileLoader('let_me_out', 'bin/let-me-out')
spec = importlib.util.spec_from_loader(loader.name, loader)
let_me_out = importlib.util.module_from_spec(spec)
loader.exec_module(let_me_out)
return let_me_out
def test_master_disaster(let_me_out):
assert let_me_out.master_disaster() == 'spam'
Python 2.7
Python 2.7 使事情变得更简单:
Python 2.7
Things are even easier with Python 2.7:
import imp
import pytest
@pytest.fixture(scope='session')
def let_me_out():
return imp.load_source('let_me_out', 'bin/let-me-out')
def test_master_disaster(let_me_out):
assert let_me_out.master_disaster() == 'spam'
这篇关于如何在pytest中导入函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!