You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that unittest.TestCase.__init__ is called once per test #64149
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=Noneclosed_at=<Date2018-05-19.00:57:20.147>created_at=<Date2013-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=<Date2018-05-19.00:57:20.147>user='https://github.com/PCManticore'
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.
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.
PCManticore commented on Dec 10, 2013
PCManticoremannequin
MannequinAuthor
More actions
"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.
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.
PCManticore commented on Dec 11, 2013
PCManticoremannequin
MannequinAuthor
More actions
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.
PCManticore commented on Apr 27, 2014
PCManticoremannequin
MannequinAuthor
More actions
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.
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
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
Activity
PCManticore commentedon Dec 10, 2013
I was surprised that in the following __init__ is actually called once per test function, although nothing in documentation implied so.
a.py
------
Ran 2 tests in 0.001s
OK
The attached patch adds a note regarding this behaviour for the TestCase class.
bitdancer commentedon Dec 10, 2013
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.
nedbat commentedon Dec 10, 2013
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.
PCManticore commentedon Dec 10, 2013
"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.
voidspace commentedon Dec 10, 2013
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.
PCManticore commentedon Dec 11, 2013
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
fixturewithinstanceand it emphasizes that the called method belongs to that particular TestCase instance.PCManticore commentedon Apr 27, 2014
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.
gpshead commentedon May 17, 2018
New changeset dff4675 by Gregory P. Smith in branch 'master':
bpo-19950: Clarify unittest TestCase instance use. (GH-6875)
dff4675
gpshead commentedon May 19, 2018
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
gpshead commentedon May 19, 2018
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