Mock object but also assert method calls?

Steven D'Aprano steve at pearwood.info
Thu Aug 13 22:53:44 EDT 2015


On Fri, 14 Aug 2015 07:21 am, Ben Finney wrote:

>> If find following totally different to the normal API which
>> is provided by the mock library:
>>
>> assert call().test2("hello") in mocked_objects.mock_calls
> 
> The ‘assert’ statement is a crude tool, which knows little about the
> intent of your assertion.

I agree with Ben here. Despite the popularity of "nose" (I think it is
nose?) which uses `assert` for testing, I think that is a gross misuse of
the statement. It is okay to use assertions this way for quick and dirty ad
hoc testing, say at the command line, but IMO totally inappropriate for
anything more formal, like unit testing.

If for no other reason than the use of `assert` for testing makes it
impossible to test your code when running with the Python -O (optimize)
switch.

For more detail on the uses, and abuses, of `assert` see this:

http://import-that.dreamwidth.org/676.html


-- 
Steven




More information about the Python-list mailing list