개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.06.11 09:37

expect.match_max 관련 질문

목록
  • 최적화마스터 오래 전 2025.06.11 09:37
  • 45
    1
안녕하세요 선생님. 파이썬의 unittest 모듈을 사용하여 테스트를 작성 중인데, expect.match_max 옵션에 대해 이해가 잘 안 가는 점이 있습니다.

expect.match_max는 일치하는 문자열의 최대 길이를 지정할 수 있는 옵션인 것 같지만, 정확한 사용법과 예시를 알려주시면 감사하겠습니다.

예를 들어, expect.match_max를 사용하여 "hello world"와 "hello"가 일치하는지 테스트하는 방법은 어떻게 해야 할까요?

아래의 예시 코드를 보시면, expect.match_max를 사용하여 테스트를 작성한 것을 볼 수 있습니다.

python

import unittest

from unittest.mock import patch



class TestHelloWorld(unittest.TestCase):

    @patch('sys.stdout')

    def test_hello_world(self, mock_stdout):

        with patch('builtins.input', return_value='hello'):

            self.assertEqual(expect.match_max('hello world', 'hello'), True)



expect.match_max를 사용하여 테스트를 작성하는 방법에 대한 설명과 예시를 알려주시면 감사하겠습니다.

    댓글목록

    profile_image
    나우호스팅  오래 전



    expect.match_max는 일치하는 문자열의 최대 길이를 지정할 수 있는 옵션이며, 사용법은 다음과 같습니다.

    expect.match_max(max_length, string, substring)

    - max_length: 일치하는 문자열의 최대 길이
    - string: 전체 문자열
    - substring: 부분 문자열

    예를 들어, "hello world"와 "hello"가 일치하는지 테스트하려면 다음과 같이 사용할 수 있습니다.

    expect.match_max(5, "hello world", "hello")

    이 코드는 "hello world"의 최대 5자리 문자열이 "hello"와 일치하는지 테스트합니다.

    위의 예시 코드에서 expect.match_max를 사용하여 테스트를 작성한 코드는 다음과 같이 수정할 수 있습니다.

    python
    
    import unittest
    
    from unittest.mock import patch
    
    
    
    class TestHelloWorld(unittest.TestCase):
    
        @patch('sys.stdout')
    
        def test_hello_world(self, mock_stdout):
    
            with patch('builtins.input', return_value='hello'):
    
                self.assertTrue(expect.match_max(5, 'hello world', 'hello'))
    
    


    이 코드는 "hello world"의 최대 5자리 문자열이 "hello"와 일치하는지 테스트합니다.

    2025-06-11 09:38

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 37,279건 / 5 페이지

검색

게시물 검색