나는 mysite/vncbrowser/tests.py
일부 단위 테스트를 넣었습니다, 나는 이러한를 실행할 수 있습니다Django에서 tests.py의 가져 오기에 사이트 이름을 추가해야하는 이유는 무엇입니까?
cd mysite
python manage.py test vncbrowser
이 tests.py
에서, 내가 가진 모델 클래스를 가져옵니다
from vncbrowser.models import Project, Stack, Integer3D, Double3D
... 그리고 삽입을 테스트 같은 테스트와 사용자 정의 필드 종류로의 Integer3D
의 : 그러나
class InsertionTest(TestCase):
def test_stack_insertion(self):
s = Stack()
s.title = "Example Stack"
s.image_base = "http://foo/bar/"
s.dimension = Integer3D(x=2048, y=1536, z=460)
s.resolution = Double3D(x=5.0001, y = 5.0002, z=9.0003)
s.save()
self.assertEqual(s.id, 1)
, 나는로 테스트를 실행할 때, models.py
소스의 isinstance(value, Integer3D)
확인에 실패했습니다. models.py
파일에서 해당 파일의 앞에 정의 된 Integer3D
에 대한 노출 된 참조는 전체 이름이 vncbrowser.models.Integer3D
인 반면 테스트에서 전달 된 개체는 전체 이름이 mysite.vncbrowser.models.Integer3D
입니다.
디버깅 문과 models.py
에서 해당 코드 :
value is <vncbrowser.models.Integer3D object at 0x22bbf90>, of type
<class 'vncbrowser.models.Integer3D'>
but a new Integer3D instance is
<class 'mysite.vncbrowser.models.Integer3D'>
isinstance check failed
걸 수
class Integer3D(object):
[... elided ...]
class Integer3DField(models.Field):
def to_python(self, value):
a = Integer3D()
print >> sys.stderr, "value is %s, of type %s" % (value, type(value))
print >> sys.stderr, "but a new Integer3D instance is", type(a)
if isinstance(value, Integer3D):
print >> sys.stderr, "isinstance check worked"
return value
print >> sys.stderr, "isinstance check failed"
는 ... 즉 (명확성을 위해 추가 된 일부 바꿈과 공백)이 출력을 생성 tests.py
에서 가져 오기를 변경하여이 테스트 작업을 수행하십시오.
from mysite.vncbrowser.models import Project, Stack, Integer3D, Double3D
... tests.py
파일에 mysite
자격이 필요한 이유는 알 수 없습니다. 내 장고 소스의 다른 곳에서는 필요하지 않은 것 같습니다. 나는 명백한 것을 놓치고 있다고 확신하지만 아마도 누군가가 설명 할 수 있을까요?
(필자는 from mysite....
수입 작품은, 사실, 난 그냥 그 문 앞에서 sys.path
를 인쇄하는 경우 때문에, 경로 /home/mark/foo/mysite/
을 포함하지만, 왜 모르겠어요하지 /home/mark/foo/
.)
내 현재 작업 디렉토리 내가 python manage.py test vncbrowser
을 실행할 때 /home/mark/foo/mysite/
입니다. 요청으로 다음과 같이
, 내 프로젝트의 레이아웃은 다음과 같습니다
── mysite
├── custom_postgresql_psycopg2
│ ├── base.py
│ └── __init__.py
├── __init__.py
├── manage.py
├── settings.py
├── urls.py
└── vncbrowser
├── __init__.py
├── models.py
├── tables.sql
├── tests.py
└── views.py
위의 __init__.py
파일의 모든
"\n".join(sys.path)
를 인쇄하면 내가 얻을 :
/home/mark/foo/mysite
/home/mark/foo/env/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg
/home/mark/foo/env/lib/python2.6
/home/mark/foo/env/lib/python2.6/plat-linux2
/home/mark/foo/env/lib/python2.6/lib-tk
/home/mark/foo/env/lib/python2.6/lib-old
/home/mark/foo/env/lib/python2.6/lib-dynload
/usr/lib/python2.6
/usr/lib64/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/usr/lib64/python2.6/lib-tk
/home/mark/foo/env/lib/python2.6/site-packages
업데이트 : lbp's answer에서 제안으로, 나는하여 다음을 추가 시도 tests.py 상단 :
import vncbrowser as vnc_one
import mysite.vncbrowser as vnc_two
print "vnc_one:", vnc_one.__file__
print "vnc_two:", vnc_two.__file__
...그 다른vncbrowser
에서오고있다 : 당신은 당신이 실제로 알고 싶은 한 가지를 알기 위해 당신의 PYTHONPATH의 전부를 알 필요가 없습니다
vnc_one: /home/mark/foo/mysite/vncbrowser/__init__.pyc
vnc_two: /home/mark/foo/mysite/../mysite/vncbrowser/__init__.pyc
가져 오기가 제대로 작동하지 않는 경우 물리적 파일 레이아웃을 설명해야합니다. 'manage.py'를 사용할 때 어떤 디렉토리에 있습니까? 현재 작업 디렉토리는 무엇입니까? 패키지 디렉토리에'__init __. py' 파일이 있습니까? –
@ S.Lott : 죄송합니다. 지금 레이아웃을 추가했습니다.나는'manage.py'를 실행할 때'mysite' 디렉토리에 있습니다. 처음에'cd mysite'에 의해 표시되기를 바랍니다. –
"에 의해"... 명시 적으로 암시 적으로보다 낫다. "Indicated by"는 놓치기 쉽습니다. 도움이 필요하면 조건을 놓치지 않게해야합니다. –