我正在检查有关导入python文件的类似问题,它似乎与我的_init_.py
文件在主要父级文件夹中的级别相比,从我的tests文件夹(其中包含所有文件我的测试文件)导入类,但不知道。也许这与此无关。突然无法使用类导入Pytest的python文件
我正在使用pytest
进行测试,并希望将多个测试分组到一个类中,以帮助我测试多个句子的解析:http://doc.pytest.org/en/latest/getting-started.html#grouping-multiple-tests-in-a-class。
现在,当来自同一文件夹中的其他文件(test_pipelines.py
)时,我尝试并运行import test_text
我无法执行此操作。这是为什么?
这里的主要问题是这个说法from test_text import UNEMPLOYMENT_SENTENCES
在test_pipelines.py
甚至没有拿起文件test_text
,更不用说UNEMPLOYMENT_SENTENCES
在里面。
test_text.py
UNEMPLOYMENT_SENTENCES = [
"The number of people out of work in Brazil is 2% of the population",
"2% of Brazil is out of work",
"2% of Brazil's citizens are jobless",
"About 9% of Brazil's citizens are out of work or looking for jobs",
"Brazil is in trouble; it has an unemployment rate of 7%",
"Brazil is facing issues - it has an unemployment % of 6%"
]
def test_sample_sentences():
"""
:param sentence_array: array
:return: tests all sentences with 3 tests
"""
for sentence in UNEMPLOYMENT_SENTENCES:
doc = Text(sentence)
test_instance = UnemploymentTestClass(doc)
test_instance.sentence_tokenization()
test_instance.entities()
test_instance.claim_candidates()
class UnemploymentTestClass:
def __init__(self,s):
"""
Initiates a test sentence
"""
self.s=s
....What follows is a list of functions that call self as an argument but don't contain 'test' at the front for pytest. e.g. def sentence_tokenization(self), def entities(self), def claim_candidates(self), using self.s as a parameter to play with.
class ComplexTestClass:
"""
Complex and more nuanced tests for specific sentences
"""
...Note, these are all @staticmethod functions which start with the word 'test_' e.g. def test_multiple_sentences().... so pytest can pick them up.
嗨@Manuel导入来自'test_text.py'相同的文件夹,所以这不是问题。我认为这个问题可能与我的课程有关。 –
试着做:'从文件导入类',也可以将这一行代码添加到'__init __。py'并修改'__all__' var,使其看起来像'__all__ = [“Class1”,“Class2”] ' –
当我从'test_pipelines.py'导入'test_text.py'时,我想要所有的类(你称之为模块)理想地被导入。即'UnemploymentTestClass','ComplexTestClass'和'UNEMPLOYMENT_SENTENCES'。 UNEMPLOYMENT_SENTENCES固定变量是最重要的,但指定具体的变量并不重要。这里的主要问题是'test_text import UNEMPLOYMENT_SENTENCES'这个语句甚至没有提到'test_text',更不用说'UNEMPLOYMENT_SENTENCES'了。 –