[Python-Dev] PEP: Consolidating names in the unittest
module (original) (raw)
Collin Winter collinw at gmail.com
Wed Jul 16 02:20:30 CEST 2008
- Previous message: [Python-Dev] PEP: Consolidating names in the `unittest` module
- Next message: [Python-Dev] PEP: Consolidating names in the `unittest` module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Tue, Jul 15, 2008 at 6:58 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
Significant updates include removing all reference to the (already-resolved) new-style class issue, adding footnotes and references, and a Rationale summary of discussion on both sides of the divide for 'assert*' versus 'fail*' names.
:PEP: XXX :Title: Consolidating names in the
unittest
module :Version: 0.2 :Last-Modified: 2008-07-15 :Author: Ben Finney <ben+python at benfinney.id.au> :Status: Draft :Type: Standards Track :Content-Type: test/x-rst :Created: 2008-07-14 :Python-Version: 2.7, 3.1 :Post-History: .. contents:: Abstract ======== This PEP proposes to consolidate the names that constitute the API of the standard libraryunittest
module, with the goal of removing redundant names, and conforming with PEP 8. Motivation ========== The normal use case for theunittest
module is to subclass its classes, overriding and re-using its functios and methods. This draws constant attention to the fact that the existing implementation fails several current Python standards: * It does not conform to PEP 8 [#PEP-8], requiring users to write their own non-PEP-8 conformant names when overriding methods, and encouraging extensions to further depart from PEP 8. * It has many synonyms in its API, which goes against the Zen of Python [#PEP-20] (specifically, that "there should be one, and preferably only one, obvious way to do it"). Specification ============= Remove obsolete names --------------------- The following module attributes are not documented as part of the API and are marked as obsolete in the implementation. They will be removed. *makeLoader
*getTestCaseNames
*makeSuite
*findTestCases
Remove redundant names ---------------------- The following attribute names exist only as synonyms for other names. They are to be removed, leaving only one name for each attribute in the API.TestCase
attributes ~~~~~~~~~~~~~~~~~~~~~~~ *assertEqual
*assertEquals
*assertNotEqual
*assertNotEquals
*assertAlmostEqual
*assertAlmostEquals
*assertNotAlmostEqual
*assertNotAlmostEquals
*assertRaises
*assert
*assertTrue
*assertFalse
Conform API with PEP 8 ---------------------- The following names are to be introduced, each replacing an existing name, to make all names in the module conform with PEP 8 [#PEP-8]. Each name is shown with the existing name that it replaces. Where function parameters are to be renamed also, they are shown. Where function parameters are not to be renamed, they are elided with the ellipse ("…") symbol. Module attributes ~~~~~~~~~~~~~~~~~defaulttestloader
ReplacesdefaultTestLoader
TestResult
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~adderror(…)
ReplacesaddError(…)
addresult(…)
ReplacesaddResult(…)
addsuccess(…)
ReplacesaddSuccess(…)
shouldstop
ReplacesshouldStop
starttest(…)
ReplacesstartTest(…)
stoptest(…)
ReplacesstopTest(…)
testsrun
ReplacestestsRun
wassuccessful(…)
ReplaceswasSuccessful(…)
TestCase
attributes ~~~~~~~~~~~~~~~~~~~~~~~_init_(self, methodname='runtest')
Replaces_init_(self, methodName='runTest')
testmethoddoc
ReplacestestMethodDoc
testmethodname
ReplacestestMethodName
failureexception
ReplacesfailureException
counttestcases(…)
ReplacescountTestCases(…)
defaulttestresult(…)
ReplacesdefaultTestResult(…)
failif(…)
ReplacesfailIf(…)
failifalmostequal(…)
ReplacesfailIfAlmostEqual(…)
failifequal(…)
ReplacesfailIfEqual(…)
failunless(…)
ReplacesfailUnless(…)
failunlessalmostequal(…)
ReplacesfailUnlessAlmostEqual(…)
failunlessequal(…)
ReplacesfailUnlessEqual(…)
failunlessraises(excclass, callableobj, *args, **kwargs)
ReplacesfailUnlessRaises(excClass, callableObj, *args, **kwargs)
runtest(…)
ReplacesrunTest(…)
setup(…)
ReplacessetUp(…)
shortdescription(…)
ReplacesshortDescription(…)
teardown(…)
ReplacestearDown(…)
FunctionTestCase
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~_init_(self, testfunc, setup, teardown, description)
Replaces_init_(self, testFunc, setUp, tearDown, description)
runtest(…)
ReplacesrunTest(…)
setup(…)
ReplacessetUp(…)
shortdescription(…)
ReplacesshortDescription(…)
teardown(…)
ReplacestearDown(…)
TestSuite
attributes ~~~~~~~~~~~~~~~~~~~~~~~~addtest(…)
ReplacesaddTest(…)
addtests(…)
ReplacesaddTests(…)
counttestcases(…)
ReplacescountTestCases(…)
TestLoader
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~sorttestmethodsusing
ReplacessortTestMethodsUsing
suiteclass
ReplacessuiteClass
testmethodprefix
ReplacestestMethodPrefix
gettestcasenames(self, testcaseclass)
ReplacesgetTestCaseNames(self, testCaseClass)
loadtestsfrommodule(…)
ReplacesloadTestsFromModule(…)
loadtestsfromname(…)
ReplacesloadTestsFromName(…)
loadtestsfromnames(…)
ReplacesloadTestsFromNames(…)
loadtestsfromtestcase(self, testcaseclass)
ReplacesloadTestsFromTestCase(self, testCaseClass)
TextTestResult
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~showall
ReplacesshowAll
adderror(…)
ReplacesaddError(…)
addfailure(…)
ReplacesaddFailure(…)
addsuccess(…)
ReplacesaddSuccess(…)
getdescription(…)
ReplacesgetDescription(…)
printerrorlist(…)
ReplacesprintErrorList(…)
printerrors(…)
ReplacesprintErrors(…)
starttest(…)
ReplacesstartTest(…)
TextTestRunner
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~makeresult(…)
ReplacesmakeResult(…)
TestProgram
attributes ~~~~~~~~~~~~~~~~~~~~~~~~~~_init_(self, module, defaulttest, argv, testrunner, testloader)
Replaces_init_(self, module, defaultTest, argv, testRunner, testLoader)
createtests(…)
ReplacescreateTests(…)
parseargs(…)
ReplacesparseArgs(…)
runtests(…)
ReplacesrunTests(…)
usageexit(…)
ReplacesusageExit(…)
Rationale ========= Redundant names --------------- The current API, with two or in some cases three different names referencing exactly the same function, leads to an overbroad and redundant API that violates PEP 20 [#PEP-20] ("there should be one, and preferably only one, obvious way to do it"). Removal ofassert*
names ---------------------------- While there is consensus support toremove redundant names
for theTestCase
test methods, the issue of which set of names should be retained is controversial. Arguments in favour of retaining only theassert*
names: * BDFL preference: The BDFL has stated [#vanrossum-1] a preference for theassert*
names. * Precedent: The Python standard library currently uses theassert*
names by a roughly 8:1 majority over thefail*
names. (Counting unit tests in the py3k tree at 2008-07-15 [#pitrou-1].) An ad-hoc sampling of other projects that useunittest
also demonstrates strong preference for use of theassert*
names [#bennetts-1]. * Positive admonition: Theassert*
names state the intent of how the code under test should behave, while thefail*
names are phrased in terms of how the code should not behave. Arguments in favour of retaining only thefail*
names: * Explicit is better than implicit: Thefail*
names state *what the function will do* explicitly: fail the test. With theassert*
names, the action to be taken is only implicit. * Avoid false implication: The test methods do not have any necessary connection with the built-inassert
statement. Even the exception raised, while it defaults toAssertionException
, is explicitly customisable via the documentedfailureexception
attribute. Choosing thefail*
names avoids the false association with either of these. This is exacerbated by the plain-boolean test using a name ofassert
(with a trailing underscore) to avoid a name collision with the built-inassert
statement. The correspondingfailif
name has no such issue. PEP 8 names ----------- Althoughunittest
(and its predecessorPyUnit
) are intended to be familiar to users of other xUnit interfaces, there is no attempt at direct API compatibility since the only code that Python'sunittest
interfaces with is other Python code. The module is in the standard library and its names should all conform with PEP 8 [#PEP-8]. Backwards Compatibility ======================= The names to be obsoleted should be deprecated and removed according to the schedule for modules in PEP 4 [#PEP-4]. While deprecated, use of the deprecated attributes should raise aDeprecationWarning
, with a message stating which replacement name should be used.
Is any provision being made for a 2to3 fixer/otherwise-automated transition for the changes you propose here?
Collin Winter
- Previous message: [Python-Dev] PEP: Consolidating names in the `unittest` module
- Next message: [Python-Dev] PEP: Consolidating names in the `unittest` module
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]