Programing

Python unittest에서 setUp ()과 setUpClass ()의 차이점은 무엇입니까?

lottogame 2020. 10. 17. 08:58
반응형

Python unittest에서 setUp ()과 setUpClass ()의 차이점은 무엇입니까?


Python 프레임 워크 에서 setUp()의 차이점은 무엇입니까 ? 설정이 다른 방법보다 한 가지 방법으로 처리되는 이유는 무엇입니까?setUpClass()unittest

내가 설정의 일부가 이루어집니다 이해하려면 setUp()setUpClass()가진뿐만 아니라, 기능 tearDown()tearDownClass().


클래스에 둘 이상의 테스트 메서드가있을 때 차이가 나타납니다. setUpClasstearDownClass전체 클래스에 대해 한 번 실행됩니다; setUp그리고 tearDown전에 각 시험 방법 후 실행됩니다.

예를 들면 :

class Example(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("setUpClass")

    def setUp(self):
        print("setUp")

    def test1(self):
        print("test1")

    def test2(self):
        print("test2")

    def tearDown(self):
        print("tearDown")

    @classmethod
    def tearDownClass(cls):
        print("tearDownClass")

이 테스트를 실행하면 다음이 인쇄됩니다.

setUpClass
setUp
test1
tearDown
.setUp
test2
tearDown
.tearDownClass

(도트는 ( .)이다 unittest테스트가 통과 할 때의 기본 출력.) 것을 관찰 setUptearDown전후에 표시 test1 하고 test2 반면에, setUpClass그리고 tearDownClass한 번만 표시 전체 테스트 케이스의 시작과 끝에서.


Python 프레임 워크 에서 setUp()의 차이점은 무엇입니까 ?setUpClass()unittest

주요 차이점 (Benjamin Hodgson의 답변에서 언급했듯이)은 setUpClass한 번만 setUp호출되고 모든 테스트 전에 호출 되는 반면 각 테스트 직전에 호출 된다는 것입니다 . (주의 : Python뿐만 아니라 다른 xUnit 테스트 프레임 워크의 동등한 메서드에도 동일하게 적용됩니다 unittest.)

로부터 unittest 문서 :

setUpClass()

A class method called before tests in an individual class are run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
    ...

and:

setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

Why would setup be handled in one method over the other?

This part of the question has not been answered yet. As per my comment in response to the answer by Gearon, the setUp method is meant for elements of the fixture that are common to all tests (to avoid duplicating that code in each test). I find this is often useful as removing duplication (usually) improves readability and reduces the maintenance burden.

The setUpClass method is for expensive elements that you would rather only have to do once, such as opening a database connection, opening a temporary file on the filesystem, loading a shared library for testing, etc. Doing such things before each test would slow down the test suite too much, so we just do it once before all the tests. This is a slight degradation in the independence of the tests but a necessary optimization in some situations. Arguably, one should not be doing such things in unit tests as it is usually possible to mock the database / filesystem / library / whatever without using the real thing. As such, I find that setUpClass is rarely needed. However, it is useful when testing the above examples (or similar) becomes necessary.

참고URL : https://stackoverflow.com/questions/23667610/what-is-the-difference-between-setup-and-setupclass-in-python-unittest

반응형