Skip to content

Document that unittest.TestCase.__init__ is called once per test #64149

Closed

Description

@PCManticore
BPO 19950
Nosy @gpshead, @nedbat, @ezio-melotti, @bitdancer, @voidspace, @PCManticore
PRs
  • bpo-19950: Clarify unittest TestCase instance use. #6875
  • [3.7] bpo-19950: Clarify unittest TestCase instance use. (GH-6875) #6938
  • [3.6] bpo-19950: Clarify unittest TestCase instance use. (GH-6875) #6939
  • Files
  • unittest_init.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-05-19.00:57:20.147>
    created_at = <Date 2013-12-10.22:34:18.643>
    labels = ['easy', '3.8', 'type-feature', '3.7', 'docs']
    title = 'Document that unittest.TestCase.__init__ is called once per test'
    updated_at = <Date 2018-05-19.00:57:20.147>
    user = 'https://github.com/PCManticore'

    bugs.python.org fields:

    activity = <Date 2018-05-19.00:57:20.147>
    actor = 'gregory.p.smith'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2018-05-19.00:57:20.147>
    closer = 'gregory.p.smith'
    components = ['Documentation']
    creation = <Date 2013-12-10.22:34:18.643>
    creator = 'Claudiu.Popa'
    dependencies = []
    files = ['33086']
    hgrepos = []
    issue_num = 19950
    keywords = ['patch', 'easy']
    message_count = 10.0
    messages = ['205864', '205866', '205867', '205869', '205872', '205891', '217247', '316943', '317085', '317086']
    nosy_count = 7.0
    nosy_names = ['gregory.p.smith', 'nedbat', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'docs@python', 'Claudiu.Popa']
    pr_nums = ['6875', '6938', '6939']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'commit review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue19950'
    versions = ['Python 3.7', 'Python 3.8']

    Activity

    1. PCManticore commented on Dec 10, 2013

      PCManticoremannequin
      MannequinAuthor

      I was surprised that in the following __init__ is actually called once per test function, although nothing in documentation implied so.

      a.py
      ------

      import unittest
      
      class A(unittest.TestCase):
         def __init__(self, *args, **kwargs):
             print('called only once!')
             super().__init__(*args, **kwargs)
      
         def test(self):
             self.assertEqual(1, 1)
       
         def test2(self):
             self.assertEqual(2, 2)
      
      if __name__ == '__main__':
         unittest.main()
      $ python a.py
      called only once!
      called only once!
      ..

      Ran 2 tests in 0.001s

      OK

      The attached patch adds a note regarding this behaviour for the TestCase class.

    2. added
      docs
      type-feature
      on Dec 10, 2013
    3. bitdancer commented on Dec 10, 2013

      @bitdancer
      Member

      I'm guessing it is that a new TestCase instance is instantiated each time an individual test is run. I don't know that this is part of the API, though, so I'm not sure how it should be documented.

    4. nedbat commented on Dec 10, 2013

      @nedbat
      Member

      This sentence seems to cover it: "Each instance of the TestCase will only be used to run a single test method, so a new fixture is created for each test." from http://docs.python.org/2/library/unittest.html

      The word "fixture" here is being used oddly, but that's the sentence.

    5. PCManticore commented on Dec 10, 2013

      PCManticoremannequin
      MannequinAuthor

      "Each instance of the TestCase will only be used to run a single test method, so a new fixture is created for each test."

      Yes, this seems to be, but it is not clear what fixture means in this context (as in "the preparation needed to perform one or *more* tests"?). Perhaps enhancing the message a little will suffice.

    6. voidspace commented on Dec 10, 2013

      @voidspace
      Contributor

      I'd be happy with a doc change from "fixture" to instance (or maybe add instance in parentheses). I'd rather a simple change than making the documentation much longer.

      The reason for a separate instance per test is for test isolation. Each test can create and modify instance attributes without them affecting other tests from the same class.

    7. PCManticore commented on Dec 11, 2013

      PCManticoremannequin
      MannequinAuthor

      How about the following:

      "Each instance of the TestCase will only be used to run a single test method from it, so a new instance is created for each test method."

      It replaces fixture with instance and it emphasizes that the called method belongs to that particular TestCase instance.

    8. PCManticore commented on Apr 27, 2014

      PCManticoremannequin
      MannequinAuthor

      In Python 3 docs there is a hint in the documentation for loadTestsFromModule:

      "This method searches module for classes derived from TestCase and creates an instance of the class for each test method defined for the class."

      The phrase with a fixture per test from Python 2 docs is gone though. It would be nice if the same explanation from loadTestsFromModule could be applied to TestCase documentation.

    9. added
      3.7 (EOL)
      3.8 (EOL)
      on Apr 25, 2018
    10. gpshead commented on May 17, 2018

      @gpshead
      Member

      New changeset dff4675 by Gregory P. Smith in branch 'master':
      bpo-19950: Clarify unittest TestCase instance use. (GH-6875)
      dff4675

    11. gpshead commented on May 19, 2018

      @gpshead
      Member

      New changeset a5f33a8 by Gregory P. Smith (Miss Islington (bot)) in branch '3.6':
      bpo-19950: Clarify unittest TestCase instance use. (GH-6875) (GH-6939)
      a5f33a8

    12. gpshead commented on May 19, 2018

      @gpshead
      Member

      New changeset 436972e by Gregory P. Smith (Miss Islington (bot)) in branch '3.7':
      bpo-19950: Clarify unittest TestCase instance use. (GH-6875) (GH-6938)
      436972e

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)3.8 (EOL)docseasytype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions