2014-05-01 4 views
1

py.test 기반 테스트 사례를 작성하려고합니다. '모듈'개체가 어떤 속성 '의 getItem'아래오류 발생 TypeError : 'module'객체에 '__getitem__'속성이 없습니다.

가 없습니다 : 내 test.py는

import os 
from sqlalchemy.ext.declarative import declarative_base 
Base = declarative_base() 

basedir = os.path.abspath(os.path.dirname(__file__)) 
CSRF_ENABLED = True 
SECRET_KEY = 'you-will-never-guess' 

SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:[email protected]:5432/app' 
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') 

하지만 오류 형식 오류를 얻고있다처럼

!flask/bin/python 

import pytest 
import config 


@pytest.fixture 
def app(request): 

SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:[email protected]:5432/app' 

config[SQLALCHEMY_DATABASE_URI] 
db.create_all() 
def fin(): 
    db.session.remove() 
    db.drop_all() 
request.addfinalizer(fin) 

def test_foo(app): 
    pass 

내 config.py 파일이 보이는 것입니다 오류 로그 추적

    ERROR at setup of test_foo 
request = <SubRequest 'app' for <Function 'test_foo'>> 

@pytest.fixture 
def app(request): 


    SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:[email protected]:5432/app' 
    config[SQLALCHEMY_DATABASE_URI] 
    TypeError: 'module' object has no attribute '__getitem__' 

test.py:20: TypeError 
+0

[6, 7, 8] 당신이 정확한 스택 추적을 공유 할 수 인쇄해야? 특정 표현식과 코드 라인을 참조 할 수 있습니다. –

+0

로그 추적이 추가되었습니다. py.test를 사용하여 setup 및 teardown 메소드를 작성하려고합니다. –

답변

3

모듈 config을 가져옵니다.따라서 목록처럼 액세스 할 수 없습니다

>>> import math 
>>> math[0] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'module' object has no attribute '__getitem__' 
>>> 

을 대신 너무 쉘에서 실행, 또는 함수로의 전달하려는 경우 sys.argv을 사용할 수

sys.argv :

myfile.py :

import sys 
print sys.argv[1:] 

Runn 보내고 :

myfile.py :

bash-3.2$ python myfile.py Hello World! 
['Hello', 'World!'] 
bash-3.2$ 

에 전달

def test(variable): 
     print variable 

실행 :

>>> import myfile 
>>> myfile[56] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'module' object has no attribute '__getitem__' 
>>> myfile.test(56) 
56 
>>> 

편집 :

test.py 업데이트 된 파일 :

!flask/bin/python 

import pytest 
import config 


@pytest.fixture 
def app(request): 

SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:[email protected]:5432/app' 

config.take(SQLALCHEMY_DATABASE_URI) 
db.create_all() 
def fin(): 
    db.session.remove() 
    db.drop_all() 
request.addfinalizer(fin) 

def test_foo(app): 
    pass 

config.py :

import os 
from sqlalchemy.ext.declarative import declarative_base 

global myvar 

def take(variable): 
    global myvar 
    myvar = variable 

print myvar #Just a test to see if it works 

Base = declarative_base() 

basedir = os.path.abspath(os.path.dirname(__file__)) 
CSRF_ENABLED = True 
SECRET_KEY = 'you-will-never-guess' 

SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:[email protected]:5432/app' 
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') 

테스트 위 :

내 더미 파일 config.py :

global myvar 

def take(variable): 
     global myvar 
     myvar = variable 

def show(): 
     global myvar 
     print myvar 

내 더미 파일 test.py :

import config 
variable = [6, 7, 8] 

config.take(variable) 
config.show() 

실행 :

bash-3.2$ python test.py 
[6, 7, 8] 
bash-3.2$ 
+0

myfile.py에 전달하면 다시 오류가 표시됩니다. 그렇지 않니? –

+0

나는 단지 myfile [56]을 호출 할 수 없다는 것을 보여주기 위해 그렇게했다. 나는 그것이 당신을 혼란시키지 않았 으면 좋겠지 만, 그것을 작품 속에서 통과 시키길 바란다. –

+0

미안하지만 나는 그것을 얻지 못했다. config.py 및 test.py 파일을 사용하여 전달하는 방법을 알려주실 수 있습니까? py.test를 사용하여 setup 및 teardown 함수를 작성하고 싶습니다. –

관련 문제