Python에서 자동 테스트를 실행하려고하는데 가져 오기 오류가 계속 발생합니다.Python Nosetests 가져 오기 오류
TestingPractice
- bin
- README.txt
- setup.py
- TestingPractice
- __init__.py
- main.py
- tests
- __init__.py
- test_main.py
을 지금 상단 TestingPractice에 CD, 실행 nosetests, 나는 심지어
TestingPractice.main
을 가져올 때, main.py에서 만든 내 방법이 선언되지 않은 것을 얻을 때 다음과 같이 내 디렉토리 계층 구조입니다 main.py :def total_hours(hours):
sum = 0
for hour in hours:
sum += hour
return sum
test_main.py :
# Test the hour computation.
from nose.tools import *
import TestingPractice.main
def test_hours():
to_test = [8,8,7,8] # 31
assert_equal(total_hours(to_test), 31)
실행 N osetests : 내가 가져 오기위한 여러 가지 경로를 시도
/Documents/Developer/Python/TestingPractice/tests/test_main.py", line 7, in test_hours
assert_equal(total_hours(to_test), 31)
NameError: global name 'total_hours' is not defined
,도 (상대 가져 오기 오류가 발생) 관련 수입도 시도 수출 PYTHONPATH =. 아무 소용이 없습니다.
보통 'test' 디렉토리에'__init __. py'를 가질 필요는 없습니다. 'nose'가 아닌'nose '는'__init __. py'가 없어도 test_main.py를 찾을 것입니다. –
'from nose.tools import *'를'from nose.tools import assert_equal'으로 변경하는 것을 고려하십시오. 코드를보다 쉽게 읽을 수있게 해주 며 일반적으로 좋은 연습입니다. 네가 네임 스페이스에 어떤 기능과 변수가 있는지. –
더 많은 고급 테스트 케이스가있을 때 이렇게 바뀌겠습니까? 이 예제에 대한 귀하의 요지를 참조하십시오,하지만 내가 assert_equal 이상을 사용하여 들어가면 여전히 수입 * 나쁜 것인가? – user3543605