[Daily Post]는 매일매일 탐구한 내용을 간략하게 기록하는 포스트입니다.
따라서 정리되지 않은 내용과 추측을 포함하고 있을 수 있습니다.
더 체계적인 형식을 갖춘 글은 해당 카테고리의 포스트를 확인해주세요 :)

Daily Study

PS

1193. 분수찾기

  • 로직이 어렵다. 구현은 쉬울 것 같다
  • 정말 재밌게 풀었다.
  • 해결완료

My Study

1. Python unittest 사용해보기

내용은 Python 공식 문서 중 unnittest - Unit test framework을 참고했습니다!

  • unittest는 test automation, sharing of setup and shutdown code for test를 도와주는 unit test framework이다
  • unittest는 object-oriented 관점에서 바라봤을 때 다음과 같은 요소들로 이루어져 있다

    • test fixture: 테스트를 수행할 때 필요한 DB, directory, server 등을 준비하는 것
    • test case: specific response to particular inputs
    • test suite: collection of test cases,test suites
    • test runner: execution of test providing outcome to the user
      • User may use graphical interface, a textual interface, or return special value
  • ⭐️pytest⭐️라는 3rd party framework를 python 공식 문서에서 추천하고 있다..!

2. Python에서 추천하는 framework, pytest 사용해보기

사용법 정리

  • ㄱ. pip를 이용하여 pytest 설치

    • pip install -U pytest로 설치
      • -U--upgrade와 같은 뜻으로, install하려는 패키지의 버전을 최신으로 특정하여 install하게 만드는 옵션이다
    • pytest --version로 설치가 정상적으로 이뤄졌는지 확인
      • pytest 7.3.1와 같이 설치한 pytest의 버전이 확인되면 정상적으로 설치된 것이다!
  • ㄴ. pytest로 테스트할 파일 생성

    • pytest는 터미널에서 실행하는 테스트 프레임워크로, 실행시 해당 터미널이 위치한 디렉토리에 대하여 test_*.py 혹은 *_test.py로 된 모든 파일에 대해 테스트를 수행한다.
    • 다음과 같은 내용의 코드를 test_PS.py에 작성했다.

      • x = 1
        
        def test_answer():
          assert x == 1
        
  • ㄷ. 터미널에서 pytest를 실행

    • 터미널의 경로를 테스트하려는 파일인 test_PS.py가 위치한 경로로 이동한다
    • 올바른 경로에서 터미널에 pytest를 입력하고 실행시킨다
    • 정상적으로 테스트를 통과한 경우, 다음과 같은 결과가 반환된다
    • snp@Seung-Giui-MacBookPro Assignments % pytest
      =================================================== test session starts ====================================================
      platform darwin -- Python 3.10.6, pytest-7.3.1, pluggy-1.0.0
      rootdir: /Users/snp/Assignments
      collected 1 item
      
      test_PS.py . [100%]
      
      ==================================================== 1 passed in 0.04s =====================================================
      
    • 정상적으로 테스트가 완료된 것을 확인할 수 있었다!