Re: [Pyunit-interest] intro & Questions
Brought to you by:
purcell
From: Steve P. <ste...@ya...> - 2001-02-15 09:27:47
|
Bill Bumgarner wrote: > While it is trivial to use PyUnit to trigger, say, JUnit > based tests internal to the application or applications, that doesn't > provide the level of testing that I need. In particular, it doesn't > test user level interaction with the site via HTTP. > [snip] > - PyUnit doesn't seem to maintain state between test runs. > I.e. if I have a TestCase subclass with 10 test cases, that results > in 10 individual instances of the TestCase subclass. Yep, this is intentional, because tests should not interfere with each other. > However, I need > to maintain state across tests-- session identifiers and the other > wonderful fallout from apps deployed against the web. I can't > imagine this is a new problem and, while my solution works, I'm > curious what others have done and if there is some kind of a > "standard" pattern. You're right -- this isn't a new problem. I recently had to solve the same thing in my day job, and I came up with a fun solution that really works nicely. I wrote a module 'httpsession' that simulates a browser session with a remote HTTP server. It tracks and sends cookies, follows redirects, allows posting of forms and can be inspected after each page request to ensure that the session is in the required state. Here's a sample test: class LoginTestCase(unittest.TestCase): ... def makeSession(self): return httpsession.HTTPSession(baseURL=self.fullURL('/')) def testLoginRequired(self): session = self.makeSession() session.get("/", redirectsAllowed=2) assert session.replycode == 200 assert session.path == self.LOGIN_PAGE assert session.query['redirect'] == self.MENU_PAGE Now, the steps required to perform a log-in could be put in the 'setUp' method of such a TestCase class: class PostLoginTestCase(unittest.TestCase): def setUp(self): self.session = self.makeLoggedInSession() def makeLoggedInSession(self): session = httpsession.HTTPSession(baseURL=self.fullURL('/')) session.get("/Login", {'login':'myuser', 'password':'mypassword'}) assert session.path == self.MENU_PAGE return session def testManagementPageVisible(self): self.session.get(self.MANAGEMENT_PAGE) assert session.replycode == 200 assert session.path == self.MANAGEMENT_PAGE Then any test methods, such as 'testManagementPageVisible' can assume that self.session is a validly logged-in session. This way, a very complete test suite can be built up, but every test case starts with a fresh browser session. Works well for me. In fact, I've just registered a new 'webunit' project on sourceforge to house the 'httpsession' module and future additional modules and documentation related to this style of testing. Nothing there yet, though, so if anyone wants the current (very imperfect) version of the 'httpsession' module, I'm happy to send it by mail or post it somewhere. > - What are people doing to automate the run of tests? If I > have a bunch of different python scripts in a directory-- each > implementing a test suite-- what is the best way to run everything in > the directory? Take a look at 'examples/alltests.py' in the distribution package. You can probably figure out a cleverer way, but it depends on how a particular project arranges its test modules. > - The examples didn't really cover spewing stuff to > stdout/stderr during the run. I don't forsee any particularly nasty > problems in doing so, but-- again-- am looking to a more experienced > community to provide some guidance. Try to avoid printing anything while your test runs. If you can't avoid it, try writing the test output to a file: if __name__ == '__main__': resultsfile = open('results.log','w') unittest.main('suite', testRunner=unittest.TextTestRunner(resultsfile)) > thanks-- very cool stuff! It has already saved me hours of > potentially very stupid debugging.... Sounds like a good thing. Best wishes, -Steve -- Steve Purcell, Pythangelist Get testing at http://pyunit.sourceforge.net/ Get servlets at http://pyserv.sourceforge.net/ "Even snakes are afraid of snakes." -- Steven Wright |