This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document that unittest.TestCase.__init__ is called once per test
Type: enhancement Stage: commit review
Components: Documentation Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Claudiu.Popa, docs@python, ezio.melotti, gregory.p.smith, michael.foord, nedbat, r.david.murray
Priority: normal Keywords: easy, patch

Created on 2013-12-10 22:34 by Claudiu.Popa, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unittest_init.patch Claudiu.Popa, 2013-12-10 22:34 review
Pull Requests
URL Status Linked Edit
PR 6875 merged gregory.p.smith, 2018-05-15 19:49
PR 6938 merged miss-islington, 2018-05-17 15:08
PR 6939 merged miss-islington, 2018-05-17 15:09
Messages (10)
msg205864 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-12-10 22:34
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.
msg205866 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-10 22:51
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.
msg205867 - (view) Author: Ned Batchelder (nedbat) * (Python triager) Date: 2013-12-10 23:00
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.
msg205869 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-12-10 23:07
"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.
msg205872 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2013-12-10 23:49
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.
msg205891 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2013-12-11 08:08
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.
msg217247 - (view) Author: PCManticore (Claudiu.Popa) * (Python triager) Date: 2014-04-27 09:59
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.
msg316943 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-05-17 15:08
New changeset dff46758f267ad6c13096c69c4e1dee17f9969aa by Gregory P. Smith in branch 'master':
bpo-19950: Clarify unittest TestCase instance use. (GH-6875)
https://github.com/python/cpython/commit/dff46758f267ad6c13096c69c4e1dee17f9969aa
msg317085 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-05-19 00:55
New changeset a5f33a899f6450de96f5e4cd154de4486d50bdd7 by Gregory P. Smith (Miss Islington (bot)) in branch '3.6':
bpo-19950: Clarify unittest TestCase instance use. (GH-6875) (GH-6939)
https://github.com/python/cpython/commit/a5f33a899f6450de96f5e4cd154de4486d50bdd7
msg317086 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-05-19 00:55
New changeset 436972e295f5057fe7cdd7312f543c2fa884705d by Gregory P. Smith (Miss Islington (bot)) in branch '3.7':
bpo-19950: Clarify unittest TestCase instance use. (GH-6875) (GH-6938)
https://github.com/python/cpython/commit/436972e295f5057fe7cdd7312f543c2fa884705d
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64149
2018-05-19 00:57:20gregory.p.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> commit review
2018-05-19 00:55:47gregory.p.smithsetmessages: + msg317086
2018-05-19 00:55:08gregory.p.smithsetmessages: + msg317085
2018-05-17 15:09:55miss-islingtonsetpull_requests: + pull_request6609
2018-05-17 15:08:57miss-islingtonsetpull_requests: + pull_request6608
2018-05-17 15:08:48gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg316943
2018-05-15 19:49:03gregory.p.smithsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request6548
2018-04-25 17:25:32cheryl.sabellasetkeywords: + easy, - patch
stage: needs patch
versions: + Python 3.7, Python 3.8, - Python 3.4
2014-04-27 09:59:39Claudiu.Popasetmessages: + msg217247
2014-02-15 15:04:42ezio.melottisetnosy: + ezio.melotti
2013-12-11 08:08:24Claudiu.Popasetmessages: + msg205891
2013-12-10 23:49:35michael.foordsetmessages: + msg205872
2013-12-10 23:07:40Claudiu.Popasetmessages: + msg205869
2013-12-10 23:00:59nedbatsetnosy: + nedbat
messages: + msg205867
2013-12-10 22:51:06r.david.murraysetnosy: + r.david.murray
messages: + msg205866
2013-12-10 22:34:18Claudiu.Popacreate