我正在檢查有關導入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'了。 –