Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python을 사용한 자동화된 소프트웨어 테스트

<시간/>

이 튜토리얼에서는 Python에서 테스트를 자동화하는 방법을 배울 것입니다. 코드를 작성한 후 다양한 유형의 입력을 제공하여 테스트하고 코드가 올바르게 작동하는지 확인해야 합니다.

수동 또는 자동으로 수행할 수 있습니다. 수동 테스트를 수행하는 것은 매우 어렵습니다. 그래서 우리는 파이썬에서 자동화된 테스트에 대해 배울 것입니다. 시작하겠습니다.

unittest라는 모듈이 있습니다. , 코드를 자동으로 테스트하는 데 사용됩니다. 우리는 이 튜토리얼에서 이 모듈로 작업할 것입니다. 초보자가 unittest를 시작하는 것은 간단합니다. 테스트용 모듈. 기초부터 코딩을 시작해 봅시다.

테스트해야 하는 방법은 테스트로 시작해야 합니다. 텍스트.

예시

# importing unittest module
import unittest
class SampleTest(unittest.TestCase):
   # return True or False
   def test(self):
      self.assertTrue(True)
# running the test
unittest.main()

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

Ran 1 test in 0.001s
OK

문자열 메서드 테스트

이제 샘플 테스트 케이스를 사용하여 다양한 문자열 메서드를 테스트할 것입니다. 메소드 이름은 test로 시작해야 합니다. .

예시

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
   # string equal
   def test_string_equality(self):
      # if both arguments are then it's succes
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   # comparing the two strings
   def test_string_case(self):
      # if both arguments are then it's succes
      self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
   # checking whether a string is upper or not
   def test_is_string_upper(self):
      # used to check whether the statement is True or False
      self.assertTrue('TUTORIALSPOINT'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

Ran 3 tests in 0.001s
OK

결론

프로그램에서 테스트를 사용하여 많은 시간을 절약할 수 있습니다. 튜토리얼에서 의문점이 있으면 댓글 섹션에 언급하세요.