Need advice on writing better test cases.

Anubhav Yadav anubhav.yadav at gmx.com
Mon Aug 28 04:03:01 EDT 2017


> On 28-Aug-2017, at 04:35, Ben Finney <ben+python at benfinney.id.au> wrote:
> 
> Anubhav Yadav <anubhav.yadav at gmx.com> writes:
> 
>> I want to write more test cases, specially that rely on database
>> insertions and reads and file IO.
> 
> Thanks for taking seriously the importance of test cases for your code!
> 
> One important thing to recognise is that a unit test is only one type of
> test. It tests one unit of code, typically a function, and should assert
> exactly one clearly true-or-false result of calling that code unit.
> 
> If you have a function and you want to assert *that function's*
> behaviour, you can avoid external dependencies during the test run by
> providing fake resources. These can be mocks (e.g. with ‘unittest.mock’)
> or other fake resources that are going to behave exactly how you want,
> for the purpose of testing the code unit.
> 
> Unit test cases:
> 
> * Exercise a small unit of code in isolation.
> * Each test exactly one obvious behavour of the code unit.
> * Aim to have exactly one reason the test case can fail.
> 
> Because they are isolated and test a small code unit, they are typically
> *fast* and can be run very often, because the entire unit test suite
> completes in seconds.
> 
>> How to test if things are going into the database properly or not?
> 
> That is *not* a unit test; it is a test that one part of your code
> has the right effect on some other part of the system. This meets the
> description not of a unit test but of an integration test.
> 
> These integration tests, because they will likely be a lot slower than
> your unit tests, should be in a separate suite of integration tests, to
> be run when the time is available to run them.
> 
> Integration tests:
> 
> * Exercise many code units together.
> * Typically make an assertion about the *resulting state* of many
>  underlying actions.
> * Can have many things that can cause the test case to fail.
> 
>> (mysql/mongo). I want to be able to create a test database environment
>> as simple as possible. Create and delete the test environment before
>> each functional test case is run.
> 
> One good article discussion how to make integration tests, specifically
> for database integration with your app, is this one
> <URL:https://julien.danjou.info/blog/2014/db-integration-testing-strategies-python>.
> 
> I hope that helps.
> 
> -- 
> \         “The enjoyment of one's tools is an essential ingredient of |
>  `\             successful work.” —Donald Knuth, _The Art of Computer |
> _o__)                                                     Programming_ |
> Ben Finney
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list