Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - Django setUpTestData() vs. setUp()

Django 1.8 shipped with a refactored TestCase which allows for data initialization at the class level using transactions and savepoints via the setUpTestData() method. This is in contrast to unittest's setUp() which runs before every single test method.

Question: What is the use case for setUp() in Django now that setUpTestData() exists?

I'm looking for objective, high-level answers only, as otherwise this question would be too broad for Stack Overflow.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not uncommon for there to be set-up code that can't run as a class method. One notable example is the Django test client: you might not want to reuse the same client instance across tests that otherwise share much of the same data, and indeed, the client instances automatically included in subclasses of Django's SimpleTestCase are created per test method rather than for the entire class. Suppose you had a test from the pre-Django 1.8 world with a setUp method like this:

    def setUp(self):
        self.the_user = f.UserFactory.create()
        self.the_post = f.PostFactory.create(author=self.the_user)
        self.client.login(
            username=self.the_user.username, password=TEST_PASSWORD
        )
        # ... &c.

You might tempted to modernize the test case by changing setUp to setUpTestData, slapping a @classmethod decorator on top, and changing all the selfs to cls. But that will fail with a AttributeError: type object 'MyTestCase' has no attribute 'client'! Instead, you should use setUpTestData for the shared data and setUp for the per-test-method client:

    @classmethod
    def setUpTestData(cls):
        cls.the_user = f.UserFactory.create()
        cls.the_post = f.PostFactory.create(author=cls.the_user)
        # ... &c.

    def setUp(self):
        self.client.login(
            username=self.the_user.username, password=TEST_PASSWORD
        )

Note: if you are wondering what that variable f is doing in the example code, it comes from factoryboy - a useful fixtures library for creating objects for your tests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...