[TIP] Asserting side effects for an integration test

Doug Hellmann doug.hellmann at gmail.com
Thu Dec 24 08:16:27 PST 2009


On Dec 24, 2009, at 10:49 AM, Shawn Dahlen wrote:

> All,
>
> I'm looking for some advice on how to assert a side effect produced  
> by testing a function. Specifically, our project has a set of  
> command objects that internally use SQLAlchemy for database access.  
> We would like to have a set of integration tests that load a fixture  
> (using the Fixture project) into the database, execute the command,  
> and assert the results. For commands that return the result, this is  
> straightforward. However, we are debating how to assert commands  
> that create, update, or delete data.
>
> Option 1: In the test, call the CreateCommand then call the  
> GetItemsCommand asserting that the item count has increased. This  
> keeps our test unaware of the implementation detail but it breaks  
> isolation for testing the CreateCommand. My current thought is that  
> this would be ok since it is an integration test.
>
> Option 2: In the test, call the CreateCommand. Subsequently, use a  
> SQLAlchemy query in the test to assert the change in database state.  
> This would keep each test isolated, but it now exposes  
> implementation details.
>
> Your advice would be appreciated. Thanks.

I would go with option 1. Create it using your API, then query for the  
thing you created using your API to make sure it is there.

Doug




More information about the testing-in-python mailing list