2017-02-16 64 views
-1

我正在檢查有關導入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_SENTENCEStest_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. 

回答

0

文件__init__.py的內容來構建你的模塊,然後將組。

如果你把你的test_text.py放在你有輸入它的腳本的文件夾中,那應該沒有問題。如果您的test_text.py位於子文件夾中,我建議添加__init__.py文件。

這是一個封裝結構會是什麼樣子:

sound/       Top-level package 
     __init__.py    Initialize the sound package 
     formats/     Subpackage for file format conversions 
       __init__.py 
       wavread.py 
       wavwrite.py 
       aiffread.py 
       aiffwrite.py 
       auread.py 
       auwrite.py 
       ... 
     effects/     Subpackage for sound effects 
       __init__.py 
       echo.py 
       surround.py 
       reverse.py 
       ... 
     filters/     Subpackage for filters 
       __init__.py 
       equalizer.py 
       vocoder.py 
       karaoke.py 
       ... 

__init__.py你可以聲明瓦爾裏面像__all__ = ["echo", "surround", "reverse"]這將導入您指定的文from sound.effects import *

PS所有子:我抄你的代碼,並沒有任何問題從相同的文件夾中導入它。

PS2:來源:https://docs.python.org/3/tutorial/modules.html#packages

的代碼塊,對我,當我在同一個文件夾中放置prueba.py(代碼段)作爲進口它的腳本作品。

from prueba import UnemploymentTestClass 
a = UnemploymentTestClass("asd") 
print a.s 

# print result 
# asd 
+0

嗨@Manuel導入來自'test_text.py'相同的文件夾,所以這不是問題。我認爲這個問題可能與我的課程有關。 –

+0

試着做:'從文件導入類',也可以將這一行代碼添加到'__init __。py'並修改'__all__' var,使其看起來像'__all__ = [「Class1」,「Class2」] ' –

+0

當我從'test_pipelines.py'導入'test_text.py'時,我想要所有的類(你稱之爲模塊)理想地被導入。即'UnemploymentTestClass','ComplexTestClass'和'UNEMPLOYMENT_SENTENCES'。 UNEMPLOYMENT_SENTENCES固定變量是最重要的,但指定具體的變量並不重要。這裏的主要問題是'test_text import UNEMPLOYMENT_SENTENCES'這個語句甚至沒有提到'test_text',更不用說'UNEMPLOYMENT_SENTENCES'了。 –