2019년 12월 15일 일요일

python unittest test method의 순서


앞에서 unittest 의 test method의 순서는 함수의 순서는 아니고 abc... 순일거라고 공유 했습니다.

예제를 들어보면,
import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()

위와 같은 예제에서 test_upper test_isupper test_split 의 테스트 순서는 절대 변경이 안된다는 의미입니다.

문서를 좀 찾아봤습니다.

https://docs.python.org/3/library/unittest.html

본문 내용중 다음과 같은 내용이 나옵니다.

Note

The order in which the various tests will be run is determined by sorting the test method names with respect to the built-in ordering for strings.

정렬된 상태라고 나옵니다. 결국 abc 이 맞다고 보면 됩니다.

Testcase의 순서를 획득하는 메소드는 getTestCaseNames 이며 prefix값이 있어야 합니다.

소스
import unittest

class TestStringMethods(unittest.TestCase):

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    print(unittest.getTestCaseNames(TestStringMethods,'test'))
    unittest.main(verbosity=2)

결과
['test_isupper', 'test_split', 'test_upper']
test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.033s

OK


댓글 없음:

댓글 쓰기