Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 100167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:31:39+00:00 2026-05-11T00:31:39+00:00

I currently have a unittest.TestCase that looks like.. class test_appletrailer(unittest.TestCase): def setup(self): self.all_trailers =

  • 0

I currently have a unittest.TestCase that looks like..

class test_appletrailer(unittest.TestCase):     def setup(self):         self.all_trailers = Trailers(res = '720', verbose = True)      def test_has_trailers(self):         self.failUnless(len(self.all_trailers) > 1)      # ..more tests.. 

This works fine, but the Trailers() call takes about 2 seconds to run.. Given that setUp() is called before each test is run, the tests now take almost 10 seconds to run (with only 3 test functions)

What is the correct way of caching the self.all_trailers variable between tests?

Removing the setUp function, and doing..

class test_appletrailer(unittest.TestCase):     all_trailers = Trailers(res = '720', verbose = True) 

..works, but then it claims ‘Ran 3 tests in 0.000s’ which is incorrect.. The only other way I could think of is to have a cache_trailers global variable (which works correctly, but is rather horrible):

cache_trailers = None class test_appletrailer(unittest.TestCase):     def setUp(self):         global cache_trailers         if cache_trailers is None:             cache_trailers = self.all_trailers = all_trailers = Trailers(res = '720', verbose = True)         else:             self.all_trailers = cache_trailers 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:31:40+00:00Added an answer on May 11, 2026 at 12:31 am

    How about using a class member that only gets initialized once?

    class test_appletrailer(unittest.TestCase):      all_trailers = None      def setup(self):         # Only initialize all_trailers once.         if self.all_trailers is None:             self.__class__.all_trailers = Trailers(res = '720', verbose = True) 

    Lookups that refer to self.all_trailers will go to the next step in the MRO — self.__class__.all_trailers, which will be initialized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can create an Application Lock to protect your flag,… May 11, 2026 at 12:36 pm
  • added an answer Well, if after setting the DataContext, you did an Invoke… May 11, 2026 at 12:36 pm
  • added an answer As others have said you can't do what you are… May 11, 2026 at 12:36 pm

Related Questions

I currently have a fairly robust server-side validation system in place, but I'm looking
I currently have a class and I'm trying to create an easy GUI to
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I currently have a DetailsView in ASP.NET that gets data from the database based
currently, I am looking deeper into testing techniques, even though I am not sure
I would like to be able to add a message to a unit test,
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and
The idea is to create a class that exposes a context but handles the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.