2017-02-16 65 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'了。 –