728x90
반응형

Django Unit Test

  • github 에서 Django 를 git clone 하면 ~/django/tests 디렉토리에 테스트 스위트가 있다.
  • 테스트 환경은 Tibero7 과 Django 5.1 버전을 연동하여 테스트해본다.
  • Django 5.1  사용하기 위해서는 Python 3.10 버전 이상을 설치해야 한다. 
  • https://docs.djangoproject.com/ko/5.1/faq/install/
 

자주묻는 질문: 설치 | Django documentation

The web framework for perfectionists with deadlines.

docs.djangoproject.com

 

 

Django UnitTest 환경 셋팅

$ git clone https://github.com/YourGitHubName/django.git django-repo
$ cd django-repo/tests
$ python -m pip install -e ..
$ python -m pip install -r requirements/py3.txt
$ ./runtests.py

 

Django UnitTest 수행

  • runtests.py  실행하여 단위 테스트를 실행할 수 있다.
  •  --settings=[테스트 환경 설정 정보] 접속 및 테스트 수행 관련 정보를 작성 한 설정 파일이다.
python runtests.py --settings=test_tibero

#특정 테스트 스위트만 수행 하고 자 할때
python runtests.py --settings=test_tibero  generic_relations

#수행된 SQL 정보를 확인하고 싶다면  --debug-sql 옵션을 추가 합니다.
./runtests.py --settings=test_tibero  queries  --debug-sql

#--verbosity=3 옵션을 적용 하면 현재 테스트 진행중인 SQL 실행 내역을 실시간으로 확인 가능 합니다. 
./runtests.py --settings=test_tibero  queries  --debug-sql --verbosity=3

(v_djangoenv) [django@my-rocky9.4:/home/django/django/tests]$ python runtests.py --settings=test_tibero  generic_relations
Testing against Django installed in '/home/django/v_djangoenv/lib/python3.10/site-packages/django' with up to 4 processes
Found 82 test(s).
Creating test database for alias 'default'...
Creating test user...
System check identified no issues (0 silenced).

Running tests...
----------------------------------------------------------------------
  test_absolute_max (generic_relations.test_forms.GenericInlineFormsetTests) ... ok (0.098s)
  test_absolute_max_with_max_num (generic_relations.test_forms.GenericInlineFormsetTests) ... ok (0.006s)
  test_can_delete_extra (generic_relations.test_forms.GenericInlineFormsetTests) ... ok (0.001s)
  test_disable_delete_extra (generic_relations.test_forms.GenericInlineFormsetTests) ... ok (0.001s)

----------------------------------------------------------------------
Ran 82 tests in 1.110s

OK

Generating XML reports...
Generated XML report: /home/django/django/tests/output/xunit/TEST-generic_relations.test_forms.GenericInlineFormsetTests-20250312185624.xml
Generated XML report: /home/django/django/tests/output/xunit/TEST-generic_relations.tests.GenericRelationsTests-20250312185624.xml
Generated XML report: /home/django/django/tests/output/xunit/TEST-generic_relations.tests.ProxyRelatedModelTest-20250312185624.xml
Generated XML report: /home/django/django/tests/output/xunit/TEST-generic_relations.tests.TestInitWithNoneArgument-20250312185624.xml
Destroying test database for alias 'default'...
Destroying test user...
Destroying test database tables...

  • ./runtests.py --help 를 통해 보다 다양한 옵션에 대한 사용법에 대해 알아 볼수 있다.

 

Django UnitTest 결과 확인

  • 테스트 수행 결과를 확인하기 위해서는 xmlrunner 패키지를 추가해서 테스트를 진행하면 된다.
  • 테스트 수행이 완료되면 TEST_OUTPUT_DIR 로 설정한 디렉토리에 xml 파일로 Reporting 결과가 남고
  • junit2html 팩키지를 통해 xml 파일을 html 파일로 변경 하면 가독성면에서 테스터가 결과를 한눈에 확인 가능 하다.
  • 또한 allure 팩키지를 사용 하면 html 파일 보다 더욱 가독성있게 테스트결과를 한눈에 확인 가능 하다 (추천)
pip install unittest-xml-reporting
pip install allure-pytest
pip install junit2html   
  • test_tibero.py 설정 추가
TEST_OUTPUT_DIR = "/home/django/django/tests/output/xunit"
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
TEST_OUTPUT_VERBOSE = 2  # 상세 출력
TEST_OUTPUT_DESCRIPTIONS = True

HTML 파일로 변경 해서 결과 확인 하는 방법

  • junit2html 를 통해 xml 결과 html 로 변경 하는 방법
    • TEST-generic_relations.tests.GenericRelationsTests-20250312034521.xml -> report.html 파일로 변경
junit2html /home/django/django/tests/output/xunit/TEST-generic_relations.tests.GenericRelationsTests-20250312034521.xml 
/home/django/django/tests/output/xunit/report.html

allure 를 사용 해서 브라우저를 통해 결과 확인 하는 방법

  • allure 최신 버전 다운로드 후 설치를 진행 한다.
wget https://github.com/allure-framework/allure2/releases/download/2.21.0/allure-2.21.0.tgz

tar -xvzf allure-2.21.0.tgz
sudo mv allure-2.21.0 /opt/allure

sudo ln -s /opt/allure/bin/allure /usr/local/bin/allure
allure --version
  • 테스트 실행 후 Allure 결과 생성
allure generate /home/django/django/tests/output/xunit -o /home/django/django/tests/output/allure_report --clean
  • 정상적으로 리포팅 결과 가 변환 되면 allure_report 디렉토리에 여러 파일들이 생성 된다 .
(v_djangoenv) [django@my-rocky9.4:/home/django/django/tests/output/xunit]$ ll
합계 28
-rw-r--r--. 1 django django  3640  3월 13 08:59 TEST-generic_relations.test_forms.GenericInlineFormsetTests-20250312185949.xml
-rw-r--r--. 1 django django 14721  3월 13 08:59 TEST-generic_relations.tests.GenericRelationsTests-20250312185949.xml
-rw-r--r--. 1 django django  1933  3월 13 08:59 TEST-generic_relations.tests.ProxyRelatedModelTest-20250312185949.xml
-rw-r--r--. 1 django django   449  3월 13 08:59 TEST-generic_relations.tests.TestInitWithNoneArgument-20250312185949.xml
  • /home/django/django/tests/output/allure_report 
(v_djangoenv) [django@my-rocky9.4:/home/django/django/tests/output/allure_report]$ ll
합계 2296
-rw-r--r--.  1 django django  776554  3월 13 08:59 app.js
drwxr-xr-x.  4 django django    4096  3월 13 09:00 data
drwxr-xr-x.  2 django django      73  3월 13 08:59 export
-rw-r--r--.  1 django django   15086  3월 13 08:59 favicon.ico
drwxr-xr-x.  2 django django     132  3월 13 08:59 history
-rw-r--r--.  1 django django     665  3월 13 08:59 index.html
drwxr-xr-x. 11 django django     137  3월 13 08:59 plugins
-rw-r--r--.  1 django django 1542785  3월 13 08:59 styles.css
drwxr-xr-x.  2 django django    4096  3월 13 09:00 widgets
  • index.html 파일을 실행 하면 결과 확인이 가능 하다.
  • http://127.0.0.1:3001/django/tests/output/allure_report/index.html 

728x90
반응형

+ Recent posts