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
  • 1 View
  • 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

Related Questions

I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase):
I have overridden the unittest.TestCase class to include some additional functionality like this class
Currently have a drop down menu that is activated on a hover (from display:none
Currently have password protection on my main and sub directories, however I'd like to
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I currently have a MySQL table like: id | friend 1 | 2 1
This answer explains how to create test cases dynamically. The answer's code: class Tests(unittest.TestCase):
I am unit testing mercurial integration and have a test class which currently creates

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.