복붙노트

[PYTHON] python TestSuite 구현하기

PYTHON

python TestSuite 구현하기

테스트 스위트에서 함께 실행하고 싶은 두 개의 테스트 케이스 (두 개의 서로 다른 파일)가 있습니다. "보통"Python을 실행하여 테스트를 수행 할 수 있지만 파이썬 단위 테스트를 실행하도록 선택하면 0 테스트가 실행됩니다. 지금 당장은 적어도 하나의 테스트를 수정하여 실행하려고합니다.

import usertest
import configtest # first test
import unittest   # second test

testSuite = unittest.TestSuite()
testResult = unittest.TestResult()
confTest = configtest.ConfigTestCase()
testSuite.addTest(configtest.suite())
test = testSuite.run(testResult)
print testResult.testsRun # prints 1 if run "normally"

내 테스트 케이스 설정 예제는 다음과 같습니다.

class ConfigTestCase(unittest.TestCase):
    def setUp(self):

        ##set up code

    def runTest(self):

        #runs test


def suite():
    """
        Gather all the tests from this module in a test suite.
    """
    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(ConfigTestCase))
    return test_suite

if __name__ == "__main__":
    #So you can run tests from this module individually.
    unittest.main()

이 작업을 올바르게 수행하려면 무엇을해야합니까?

해결법

  1. ==============================

    1.당신은 testuit를 사용하고자합니다. 따라서 unittest.main ()을 호출 할 필요가 없습니다. testsuit의 사용은 다음과 같아야합니다 :

    당신은 testuit를 사용하고자합니다. 따라서 unittest.main ()을 호출 할 필요가 없습니다. testsuit의 사용은 다음과 같아야합니다 :

    #import usertest
    #import configtest # first test
    import unittest   # second test
    
    class ConfigTestCase(unittest.TestCase):
        def setUp(self):
            print 'stp'
            ##set up code
    
        def runTest(self):
            #runs test
            print 'stp'
    
    def suite():
        """
            Gather all the tests from this module in a test suite.
        """
        test_suite = unittest.TestSuite()
        test_suite.addTest(unittest.makeSuite(ConfigTestCase))
        return test_suite
    
    mySuit=suite()
    
    runner=unittest.TextTestRunner()
    runner.run(mySuit)
    
  2. ==============================

    2.로더와 스위트를 만드는 모든 코드는 필요하지 않습니다. 테스트를 작성하여 원하는 테스트 러너를 사용하여 테스트 검색을 통해 실행할 수 있도록해야합니다. 이는 표준 방식으로 메서드를 명명하고, 가져올 수있는 장소에 넣거나 (또는 ​​해당 항목을 포함하는 폴더를 러너에 전달하는) unittest.TestCase에서 상속받는 것을 의미합니다. 이 작업을 마친 후에는 python -m unittest를 사용하여 가장 단순한 테스트 나 더 나은 제 3 자 테스트를 통해 테스트를 찾아서 실행할 수 있습니다.

    로더와 스위트를 만드는 모든 코드는 필요하지 않습니다. 테스트를 작성하여 원하는 테스트 러너를 사용하여 테스트 검색을 통해 실행할 수 있도록해야합니다. 이는 표준 방식으로 메서드를 명명하고, 가져올 수있는 장소에 넣거나 (또는 ​​해당 항목을 포함하는 폴더를 러너에 전달하는) unittest.TestCase에서 상속받는 것을 의미합니다. 이 작업을 마친 후에는 python -m unittest를 사용하여 가장 단순한 테스트 나 더 나은 제 3 자 테스트를 통해 테스트를 찾아서 실행할 수 있습니다.

  3. ==============================

    3.나는 두 개의 테스트를 통합 한 모듈에 대해 파이썬 단위 테스트를 실행한다고 언급하고 있다고 가정합니다. 즉, 해당 모듈에 대한 테스트 케이스를 작성하면 작동합니다. unittest.TestCase를 서브 클래 싱하고 'test'라는 단어로 시작하는 간단한 테스트를 갖는 것.

    나는 두 개의 테스트를 통합 한 모듈에 대해 파이썬 단위 테스트를 실행한다고 언급하고 있다고 가정합니다. 즉, 해당 모듈에 대한 테스트 케이스를 작성하면 작동합니다. unittest.TestCase를 서브 클래 싱하고 'test'라는 단어로 시작하는 간단한 테스트를 갖는 것.

    e.

    class testall(unittest.TestCase):
    
        def test_all(self):           
            testSuite = unittest.TestSuite()
            testResult = unittest.TestResult()
            confTest = configtest.ConfigTestCase()
            testSuite.addTest(configtest.suite())
            test = testSuite.run(testResult)
            print testResult.testsRun # prints 1 if run "normally"
    
    if __name__ == "__main__": 
          unittest.main()
    
  4. ==============================

    4.수동으로 TestCases를 수집하려는 경우 유용합니다. unittest.loader.findTestCases () :

    수동으로 TestCases를 수집하려는 경우 유용합니다. unittest.loader.findTestCases () :

    # Given a module, M, with tests:
    mySuite = unittest.loader.findTestCases(M)
    runner = unittest.TextTestRunner()
    runner.run(mySuit)
    
  5. from https://stackoverflow.com/questions/12011091/trying-to-implement-python-testsuite by cc-by-sa and MIT license