[TIP] Asserting side effects for an integration test

C. Titus Brown ctb at msu.edu
Thu Dec 24 08:18:49 PST 2009


On Thu, Dec 24, 2009 at 11:16:27AM -0500, Doug Hellmann wrote:
> 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.

What if your API is systematically lying to you?

:)

I'd probably go with #2, unless you find that you're changing implementation
details a lot.

--titus
-- 
C. Titus Brown, ctb at msu.edu



More information about the testing-in-python mailing list