[Twisted-Python] Re: [Twisted-commits] r18220 - Make tests clean up after themselves
On Thu, 21 Sep 2006 21:32:47 -0600, Jonathan Lange
Author: jml Date: Thu Sep 21 21:32:47 2006 New Revision: 18220
Modified: trunk/twisted/web/test/test_distrib.py trunk/twisted/web2/test/test_static.py trunk/twisted/words/test/test_service.py Log: Make tests clean up after themselves
* Fixes #1883 * Author: jml * Reviewer: MFen
Although not apparent to the naked eye, these tests previous did not correctly clean up after themselves. Instead, they relied on a couple of spins of the reactor to do their cleaning for them. This has been corrected.
Modified: trunk/twisted/web/test/test_distrib.py ============================================================================== --- trunk/twisted/web/test/test_distrib.py (original) +++ trunk/twisted/web/test/test_distrib.py Thu Sep 21 21:32:47 2006 @@ -15,16 +15,26 @@ self.logFile.close() del self.logFile
+ +class PBServerFactory(pb.PBServerFactory):
Docstring?
+ def buildProtocol(self, addr): + self.proto = pb.PBServerFactory.buildProtocol(self, addr) + return self.proto + + class DistribTest(unittest.TestCase): port1 = None port2 = None sub = None
def tearDown(self):
Docstring?
+ dl = [defer.Deferred(), defer.Deferred()] + self.f1.proto.notifyOnDisconnect(lambda: dl[0].callback(None)) if self.sub is not None: + self.sub.publisher.broker.notifyOnDisconnect( + lambda: dl[1].callback(None)) self.sub.publisher.broker.transport.loseConnection() http._logDateTimeStop() - dl = [] if self.port1 is not None: dl.append(self.port1.stopListening()) if self.port2 is not None: @@ -36,8 +46,8 @@ r1 = resource.Resource() r1.putChild("there", static.Data("root", "text/plain")) site1 = server.Site(r1) - f1 = pb.PBServerFactory(distrib.ResourcePublisher(site1)) - self.port1 = reactor.listenTCP(0, f1) + self.f1 = PBServerFactory(distrib.ResourcePublisher(site1)) + self.port1 = reactor.listenTCP(0, self.f1) self.sub = distrib.ResourceSubscription("127.0.0.1", self.port1.getHost().port) r2 = resource.Resource()
Modified: trunk/twisted/web2/test/test_static.py ============================================================================== --- trunk/twisted/web2/test/test_static.py (original) +++ trunk/twisted/web2/test/test_static.py Thu Sep 21 21:32:47 2006 @@ -11,7 +11,6 @@ self.text = "Hello, World\n" self.data = static.Data(self.text, "text/plain")
- def test_dataState(self): """ Test the internal state of the Data object @@ -42,7 +41,6 @@ http_headers.MimeType("text", "plain")) def checkStream(data): self.assertEquals(str(data), self.text) - return stream.readStream(iweb.IResponse(self.data.render(None)).stream, checkStream)
@@ -58,7 +56,8 @@ maxBytes=16) self.root.addSlash = True
- def uploadFile(self, fieldname, filename, mimetype, content, resrc=None, host='foo', path='/'): + def uploadFile(self, fieldname, filename, mimetype, content, resrc=None, + host='foo', path='/'): if not resrc: resrc = self.root
@@ -102,32 +101,33 @@ return d
def test_enforcesMaxBytes(self):
Docstring?
- return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename', - 'text/html', 'X'*32), - (200, {}, 'exceeds maximum length')) + return self.assertInResponse( + self.uploadFile('FileNameOne', 'myfilename', 'text/html', 'X'*32), + (200, {}, 'exceeds maximum length'))
def test_enforcesMimeType(self):
Docstring?
- return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename', - 'application/x-python', 'X'), - (200, {}, 'type not allowed')) + return self.assertInResponse( + self.uploadFile('FileNameOne', 'myfilename', + 'application/x-python', 'X'), + (200, {}, 'type not allowed'))
def test_invalidField(self):
Docstring?
- return self.assertInResponse(self.uploadFile('NotARealField', 'myfilename', - 'text/html', 'X'), - (200, {}, 'not a valid field')) + return self.assertInResponse(
Docstring?
+ self.uploadFile('NotARealField', 'myfilename', 'text/html', 'X'), + (200, {}, 'not a valid field'))
def test_reportFileSave(self):
Docstring?
- return self.assertInResponse(self.uploadFile('FileNameOne', 'myfilename', - 'text/plain', - 'X'), - (200, {}, 'Saved file')) + return self.assertInResponse( + self.uploadFile('FileNameOne', 'myfilename', 'text/plain', 'X'), + (200, {}, 'Saved file'))
def test_compareFileContents(self):
Docstring?
def gotFname(fname): contents = file(fname, 'r').read() self.assertEquals(contents, 'Test contents') - - return self.uploadFile('FileNameOne', 'myfilename', 'text/plain', - 'Test contents').addCallback( - self.fileNameFromResponse - ).addCallback(gotFname) + + d = self.uploadFile('FileNameOne', 'myfilename', 'text/plain', + 'Test contents') + d.addCallback(self.fileNameFromResponse) + d.addCallback(gotFname) + return d
Modified: trunk/twisted/words/test/test_service.py ============================================================================== --- trunk/twisted/words/test/test_service.py (original) +++ trunk/twisted/words/test/test_service.py Thu Sep 21 21:32:47 2006 @@ -875,21 +875,26 @@ self.portal = portal.Portal( self.realm, [self.checker]) self.serverFactory = pb.PBServerFactory(self.portal) + self.serverFactory.protocol = self._protocolFactory self.serverFactory.unsafeTracebacks = True self.clientFactory = pb.PBClientFactory() self.clientFactory.unsafeTracebacks = True - self.serverPort = reactor.listenTCP(0, self.serverFactory) self.clientConn = reactor.connectTCP( '127.0.0.1', self.serverPort.getHost().port, self.clientFactory)
+ def _protocolFactory(self, *args, **kw):
Docstring?
+ self._serverProtocol = pb.Broker(0) + return self._serverProtocol
def tearDown(self):
Docstring?
+ d3 = Deferred() + self._serverProtocol.notifyOnDisconnect(lambda: d3.callback(None)) return DeferredList([ maybeDeferred(self.serverPort.stopListening), - maybeDeferred(self.clientConn.disconnect)]) + maybeDeferred(self.clientConn.disconnect), d3])
def _loggedInAvatar(self, name, password, mind): creds = credentials.UsernamePassword(name, password)
_______________________________________________ Twisted-commits mailing list Twisted-commits@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-commits
On Fri, 22 Sep 2006 08:58:36 -0400, Jean-Paul Calderone
Docstring?
I'd like to direct all reviewers' attention at this point to the excellent wiki page that Chris and JP wrote a little while ago: http://twistedmatrix.com/trac/wiki/ReviewProcess Especially to this section: """ A reviewer must reject a set of changes in any of the following circumstances: * The test suite fails on any of the supported* platforms as a result of the changes. * The changes alter the behavior of code in a way which is not tested. * The changes add new behavior without adding test coverage for that behavior. * The changes modify public APIs which are undocumented (ie, a function which has no docstring is modified, or a class which has no docstring has methods added to it) without adding documentation. * The changes modify the behavior of a documented API without updating the documentation. * The changes violate the coding standard. """
* The changes modify public APIs which are undocumented (ie, a function
which has no docstring is modified, or a class which has no docstring has
methods added to it) without adding documentation.
Since when are tests public APIs?
C
On 9/22/06, glyph@divmod.com
On Fri, 22 Sep 2006 08:58:36 -0400, Jean-Paul Calderone < exarkun@divmod.com> wrote:
Docstring?
I'd like to direct all reviewers' attention at this point to the excellent wiki page that Chris and JP wrote a little while ago:
http://twistedmatrix.com/trac/wiki/ReviewProcess
Especially to this section:
"""
A reviewer must reject a set of changes in any of the following circumstances:
* The test suite fails on any of the supported* platforms as a result of the changes. * The changes alter the behavior of code in a way which is not tested. * The changes add new behavior without adding test coverage for that behavior. * The changes modify public APIs which are undocumented (ie, a function which has no docstring is modified, or a class which has no docstring has methods added to it) without adding documentation. * The changes modify the behavior of a documented API without updating the documentation. * The changes violate the coding standard.
"""
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Fri, 22 Sep 2006 08:12:20 -0700, Cory Dodt
* The changes modify public APIs which are undocumented (ie, a function which has no docstring is modified, or a class which has no docstring has methods added to it) without adding documentation.
Since when are tests public APIs?
All test methods must have their intent documented. I'll clarify this section on the wiki. Jean-Paul
On Fri, 22 Sep 2006 08:12:20 -0700, Cory Dodt
* The changes modify public APIs which are undocumented (ie, a function which has no docstring is modified, or a class which has no docstring has methods added to it) without adding documentation.
Since when are tests public APIs?
Hmm, good point, I guess "public API" is a bit misleading there. Tests also need docstrings to explain what they're testing, and what code they're supposed to cover. The test docstrings need to explain the verified constraints of the public API, or to explicitly say "this is not testing a public API" - although the vast majority of tests are verifying at least some behavior of a public API, although not directly. I'll update that document a bit later.
I'm all for having more documentation. When I reviewed that bug I went by
the strictest interpretation of ReviewProcess, which it seems wasn't clear.
I'm glad it'll get clarified.
Thanks!
C
On 9/22/06, glyph@divmod.com
On Fri, 22 Sep 2006 08:12:20 -0700, Cory Dodt
wrote: * The changes modify public APIs which are undocumented (ie, a function which has no docstring is modified, or a class which has no docstring has methods added to it) without adding documentation.
Since when are tests public APIs?
Hmm, good point, I guess "public API" is a bit misleading there.
Tests also need docstrings to explain what they're testing, and what code they're supposed to cover. The test docstrings need to explain the verified constraints of the public API, or to explicitly say "this is not testing a public API" - although the vast majority of tests are verifying at least some behavior of a public API, although not directly.
I'll update that document a bit later.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
Cory Dodt
-
glyph@divmod.com
-
Jean-Paul Calderone