장고 단위 테스트를 여러 파일로 분산시키는 방법은 무엇입니까?
- 파이썬 장고 응용 프로그램이 있습니다
- 단위 테스트 프레임 워크를 사용하고 있습니다
- 테스트는 모듈 디렉토리의 "tests.py"파일에 배치됩니다.
- 나는 통해 테스트를 실행하고 있습니다
./manage.py test app
지금..
tests.py
파일이 오히려 큰 / 복잡한지고 / 지저분한tests.py
더 작은 테스트 모음 으로 나누고 싶습니다 ...
어떻게?
Django 1.6에서는 동작이 변경되었으므로 더 이상 패키지를 만들 필요가 없습니다. 파일 이름을 지정하십시오 test*.py
.
테스트를 실행할 때 테스트 유틸리티의 기본 동작은 이름이 test로 시작하는 파일에서 모든 테스트 케이스 (즉, unittest.TestCase의 서브 클래스)를 찾고 해당 테스트 케이스에서 자동으로 테스트 스위트를 빌드하는 것입니다. 그 스위트를 실행하십시오.
에서 장고 1.6 문서 ,
테스트 감지는 unittest 모듈의 내장 테스트 감지를 기반으로합니다. 기본적으로 현재 작업 디렉토리 아래의 "test * .py"라는 파일에서 테스트를 발견합니다.
Django 1.5 설명서의 이전 동작 :
테스트를 실행할 때 테스트 유틸리티의 기본 동작은 models.py 및 tests.py에서 모든 테스트 케이스 (즉, unittest.TestCase의 서브 클래스)를 찾고 해당 테스트 케이스에서 테스트 스위트를 자동으로 빌드하는 것입니다. 그 스위트를 실행하십시오.
모듈의 테스트 스위트를 정의하는 두 번째 방법이 있습니다. models.py 또는 tests.py에서 suite ()라는 함수를 정의하면 Django 테스트 러너는 해당 함수를 사용하여 해당 모듈의 테스트 스위트를 구성합니다. 이것은 제안 된 단위 테스트 조직을 따릅니다. 복잡한 테스트 스위트를 구성하는 방법에 대한 자세한 내용은 Python 설명서를 참조하십시오.
이 방법은 Django 1.6에서 더 이상 유효하지 않습니다 . 이 게시물을 참조하십시오 .
내부가있는 tests
폴더를 만들어 ___init___.py
패키지로 만들 수 있습니다 . 그런 다음 분할 테스트 .py 파일을 추가하고에서 모든 파일을 가져옵니다 ___init___.py
.
즉, test.py
파일처럼 보이고 작동하는 모듈로 파일을 대체 하십시오.
tests
해당 앱 아래 에 디렉토리 만들기
앱 app \ models.py app \ views.py 앱 \ 테스트 app \ tests \ __ init__.py app \ tests \ bananas.py app \ tests \ apples.py
서브 모듈을 app\tests\__init__.py
다음 으로 가져옵니다 .
from bananas import *
from apples import *
이제 ./manage.py를 모두 단일 파일에있는 것처럼 사용할 수 있습니다.
./manage.py test app.some_test_in_bananas
Tomasz의 답변이 정확합니다. 그러나 가져 오기가 __init__.py
파일 구조와 일치 하는지 확인하는 것은 지루할 수 있습니다 .
폴더의 모든 테스트 를 자동으로 감지 하려면 다음을 추가하십시오 __init__.py
.
import unittest
def suite():
return unittest.TestLoader().discover("appname.tests", pattern="*.py")
이렇게하면 실행할 수 ./manage.py test appname
있지만 특정 테스트 실행은 처리 할 수 없습니다. 이를 위해이 코드를 사용할 수도 있습니다 ( __init__.py
) :
import pkgutil
import unittest
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
exec ('%s = obj' % obj.__name__)
이제 모든 테스트를 통해 manage.py test app
또는 특정 테스트를 통해 실행할 수 있습니다manage.py test app.TestApples
다음과 같이 디렉토리 구조를 만드십시오.
myapp/
__init__.py
tests/
__init__.py
test_one.py
test_two.py
...
...
그리고 python manage.py test myapp
예상대로 작동합니다.
http://docs.python.org/library/unittest.html#organizing-tests talks about splitting the files into modules, and the section right above it has an example.
If you have a more complicated setup, or don't want to use from ... import *
-type statements, you can define a function called suite
in your tests.py (or tests/__init__.py), which returns an instance of unittest.TestSuite
.
No need to code anything in init. Just create a subdirectory in your app. Only requirement is not to call it tests* For exemple
app/
app/__init_.py
app/serializers.py
app/testing/
app/testing/__init__.py
app/testing/tests_serializers.py
With Django 2.2 a simple and fairly good solution could be to create a test
folder inside an app, and you can put your related test_...py
files into, just add __init__.py
to the test
folder.
I think ./manage.py test
simply does running all the tests trick (in django >= 1.7).
If your organizing tests is about grouping and cherrypicking and you are fan of nose
use django nose:
python manage.py test another.test:TestCase.test_method
If you know nose, then you know how to "wildcard" much nicer over all your files.
PS
It is just a better practice. Hope that helps. The answer was borrowed from here: Running a specific test case in Django when your app has a tests directory
참고URL : https://stackoverflow.com/questions/6248510/how-to-spread-django-unit-tests-over-multiple-files
'Programing' 카테고리의 다른 글
오류 메시지 : " 'chromedriver'실행 파일이 경로에 있어야합니다" (0) | 2020.07.17 |
---|---|
Resharper- 사용하지 않는 모든 클래스 찾기 (0) | 2020.07.17 |
Java8 : java.lang.Object에서 메소드의 기본 메소드를 정의하는 것이 금지 된 이유 (0) | 2020.07.17 |
기능적 언어에서 '패턴 일치'란 무엇입니까? (0) | 2020.07.17 |
다른 컨트롤러에서 지시어 컨트롤러의 메소드 호출 (0) | 2020.07.17 |