Today we would like to present to you our new blog category – Junior Developers’ Notes (JDN). In this category we will publish articles written by our Junior Developers who would love to share what they have learned at Pragmatic Coders.
In the first article of JDN cycle our Junior Developer Maciej Wiśniewski describes his first impressions and experience on Mock and PyTest.
Please comment on Maciej’s article and give us your feedback. Have a good reading.
Mocking the PyTest and PyTesting The Mock
On the beginning, I have to admit that the idea of a Test-Driven Development (TDD) is not a new one. But it doesn’t mean that we have to reject it in search of some new breakthrough tool. There are things in our lives (personal and professional one) that are core ones, basics. And TDD is one of those.
It is like a dental check-up. We do it on a regular basis and don’t wait till the moment we start losing our teeth. This example may be too literal, but that’s exactly how it works with TDD.
In Pragmatic Coders we work with Python. And TDD is a part of our working process. Experience proved that testing is the best way to avoid the time-consuming process of looking for bugs and then fixing them.
The scheme is as simple as a pie. Build a test to take, write some lines of code to pass the test, refactor your code. If the result is not satisfactory enough – repeat steps above until you perform a highest standard of work.
It might take you time and seem to be redundant at some point (doing/redoing activity). But in the end, you will realize that it’s a lesson to be learned. TDD will save you loads of time and bring needed quality to a result you deliver.
We all know how to run a test. One simple command, few seconds and “ta dah” kind of moment if the screen is peacefully green or “oh, no” moment when there’s an error and it’s bellicosely red. Why is PyTest unique?
PyTest collects all test functions taking as an argument directory, filename or single test ID. What you need to provide is test_ prefix or _test suffix in filename. Test classes without an __init__ method or test_ prefixed test functions in a specified file. Convenient, simple, effective.
Moreover, PyTest offers registered fixtures as a test function decorators. It may be used as simple setters, function arguments or as mocking object facilitation.
I will focus on the details in my next article.
Mock – first look
At the very beginning of this text I have mentioned “mock”. And I did it for a reason. Supported by PyTest yield.fixture, library allows us to replace some parts of the code with mock objects to make assertions with no worries about database integrity.
We record what to do, pass the test and replay action on a real object. Again – convenient, simple, effective.
Approaching to Agile
There is a lot to be learned about agile techniques, tools, ideas, and theories. There is Continuous Integration, Control Version Systems, plenty of Quality Assurance tips and tricks. Refactoring doesn’t refer to our code only.
There is definitely a point in refactoring skills, knowledge and information that you have. The more we learn the more organized our knowledge becomes.
For sure 100% tests coverage of code looks professionally, but our tests are meant to test some real functions and features, so don’t worry if you are missing some. We all are in the process of learning.
Read more articles from Junior Developer Notes category PEP 8, and why is it important?