RFC: Additional functionality for PyUnit

June Kim juneaftn at orgio.net
Wed Aug 29 23:42:12 EDT 2001


[snip]
> Here's my further example.  I have some code I need to unit test that
> basically does a create, append, and modify to some data in a file.
> 
> Test Case:
> 
> Step 1: create the directory to run the test in.  This is a
> global setup step.
> 
> Step 2: Test the create code.  Step 1 must have been run.
> 
> Step 3: Test the append code.  Steps 1 and 2 must have been run.
> 
> Step 4: Test the modify code.  Steps 1 and 2 must have been run.
> 
> Step 5: Delete the test directory.  This is a global teardown step.
> 
> 
> Step 1 creates the directory that is needed by all of the subsequent
> steps.  Step 2 creates a file which is then needed by Steps 3 and 4,
> which means that I can't delete the test directory for each test,
> unless I want to have to run Steps 1 and 2 as part of the setup for
> Steps 3 and 4.

Each unit test should be independent from the others. If you want to 
test a scenario A->B->C->D, either you are thinking about an 
acceptance test, or you should break up the scenario into units A, B, C 
and D and test each one independently first(yeah, you must test each 
unit first before any acceptance tests) If you need to run A and B 
before C in order to test C, don't run through A and B directly, but 
think about the state after their running, and try to build up the 
state(don't compute it through). It could be a mock-up object, hard-coded
inline, whatever.

> 
> Now, in this example, Steps1 and 2 are simple, cheap, and quick to
> run.  But just imagine a situation where Step 1 was 'create an SQL
> database with all tables, columns, and relationships', and Step 2 was
> 'write multimegabytes of setup data to the database'.
> 

Bad smell unless you are doing it for acceptance tests. 

> So, what I really want is for Steps 1 and 5 to be done 'globally',
> that is, I need to create a setup state that persists across all of
> the individual tests contained in the test case, then tear down that
> setup only when all of the dependent tests have been done.
> 
> If PyUnit were to be extended to add a 'globalSetUp' method, run once
> before all other test methods in the test case, and a
> 'globalTearDown', run once after all other test methods in the test
> case,
> that are in addition to the currently-supported 'setUp' and 'tearDown'
> methods, I'd be very happy.
> 
> Or am I missing something that would already give me what I want?

If PyUnit could be more extended without hurting its original 
functionality, it's okay. But it seems like the problem is rather on how 
we use it(or how we understand what test case is) than what it is.

Best wishes,
June



More information about the Python-list mailing list