Need advice on writing better test cases.

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


> 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.

Yes I have been learning how to mock systems using `unittest.mock` package.
I learned how to patch the `requests.get` method to just return true with a fixed json
so that I could run my tests cases without relying on the actual server to be running. 

I think it’s important to read more and more examples on how different libraries and systems
are mocked and patched. 

> 
> 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.

Is there are standard or structure as to how these integration tests are written? 
Do we just define different classes for unit tests and different classes for integration tests? 

Is there any library that can help with writing integration tests? 

Also how are integration tests different than functional tests? I also want to have a good understanding of 
functional tests so that I can start writing them first before I start writing my code. This way whatever changes
I make my tests will always tell me if the required responsibility of the system if fulfilled or not. Later I would like
to run these tests cases automatically when I push my code to GitHub/Gitlab?
 
> 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.

I tried to insert some data in a sqlite database and then ran some code and then again ran some sql queries to see if 
the resulting state is what I intend it to be or not. As the code base grew I realised that it’s a pain to write so many sql 
queries manually. Is there a framework or library that I can use to simplify this? 

> 
>> (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>.

Thank you for the link. Much much helpful. My problem is that I have to write lot of test cases to insert dummy data 
into the database and then test if the data is changed properly or not. I wanted to know the industry standards and the 
general practise of doing things. 

Thanks a lot for your reply. Much much appreciated and helpful. If you can mention some books or videos to watch it will help a lot. 

Cheers. 




More information about the Python-list mailing list