I have spent a couple hours today writing unit tests. This is something I started doing recently and had a huge impact on my work and on my overall coding experience.
- Increase your confidence in your code by introducing tests.
- Manual testing is not efficient and not reliable.
- Don’t forget to run the tests regularly.
- Apply best practices when structuring your code.
- Never check in untested or broken code to Git master branch.
Testing networks, testing code
In the beginning of my career, I had difficulties to mentally deal with the network changes I had to do. I wanted to have a bulletproof plan to verify the impact of my changes. The obvious checks were easy. Is the thing I wanted to change actually working?
However, sometimes you fix something, you also break something. Knowing the protocols and the technology is a good start, but you may hit bugs in code. An this point I was lost. How to test for all of these things?
I found myself in a similar situation when I started working on more complex software projects. I touch one part, another part breaks. There are tools and good practices that you can use to make sure, your code is working and easy to maintain.
The reason I got into network automation is to do better what I was doing as a network engineer. Most people, however, have a great concern about trusting automated systems to configure network devices, especially at scale. I am one of those people. I take it very seriously. The only way to overcome this fear is to build up the confidence in myself and in my code by testing it regularly and thoroughly. I always think of different ways to challenge my scripts, what happens if… and prepare for these scenarios.
I came across PEP 20 (Python Enhancement Proposals) that lists very good ideas. It doesn’t matter if you haven’t started coding yet, take a look now.
In DevOps, CD/CI pipelines are heavily relying on different types of tests. These allow large companies to perform dozens of software releases every day without causing any problems. (hopefully!)
If you write code, it’s the best to keep it in a code repository (git), so others can make use of it and collaborate. Respect them and their time by merging only tested and working code to master branch. Use feature branches for anything you’re working on right now. It is also possible to enforce that only tested code can be merged onto master. Read my article on Git here.
You write code to work faster and smarter. This means, you need to be able to test your code fast and smart as well. Manually checking every scenario every single time you change something is not scalable, not reliable and booooring. Not checking is dangerous. Once you learnt basic coding principles, it’s worth investing the energy on how to do testing properly.
Warning! Software testing is a huge topic in itself, but you can easily add some unit tests to your code as a minimum.
Simpler is better
Code testing forces you to write simpler and more modular code. The simpler the code the easier understand and to test.
What shall I do next?
- I recommend you look at unit tests. This is a great article.
- Read about TDD – Test Driven Development
- Write a couple of tests for your own code and let me know below how you found it in the comments.
- Visit Peter’s testsmarter blog
Don’t miss out and stay motivated by subscribing to email alerts (right on desktop, bottom in mobile) or like our Facebook page.
Till next time…