Coming from the network engineering world, I had not a single thought what Test Driven Development (TDD) was.
We just do it live, right?
All jokes aside, as network engineers, we test out our configuration in a lab environment. Similarly for the DevNet Associate, you’ll need to understand what Test Driven Development is.
Do you want to do some testing?
TDD is a software development process where small unit tests are created for features that are being written. What is being tested are specific test cases to requirements.
The process looks like this:
- Create a test
- Run tests for failure
- Write code
- Run tests
- Refactor code
Another term new to me is refactoring code. It means editing and cleaning up the code without changing the function of the code.
To fully understand what this means, I am taking an example from Code Like A Girl. We’re going to use an API, Genderize.io, and write code to determine if a name is a female or male gender.
Unit Testing
To perform some unit tests, we’re going to use a library called pytest
. It’s a framework that makes writing small tests easy.
So first, we install pytest
:
pip3 install pytest
Taking an example from https://code.likeagirl.io/in-tests-we-trust-tdd-with-python-af69f47e6932
We’re going to create two files. One file contains the code you’re writing. Another file contains the unit test running against the code.
Let’s create our unit test file, test_gender.py
file:
from gender import *
import pytest
def test_should_return_female_when_the_name_is_from_female_gender():
detector = GenderDetector()
expected_gender = detector.run(‘Anna’)
assert expected_gender == 'male'
From what I’ve learned with this unit test, a function is created to take the name, Anna, and use it to get the gender for that name from Genderize. And the expected gender stated is male. Obviously, this should fail.
Here is the Python file which defines a function to request the gender of the supplied name from our unit test, test_gender.py
. Create the actual test code, gender.py
:
import requests
class GenderDetector(object):
def run(self, name):
result = requests.get('https://api.genderize.io/?name={}'.format(name)).json()
return result['gender']
In iTerm2, I run pytest
to run my unit test. I am expecting a failure. We are getting the gender of the name Elsa (which will return as female) but our unit test is expecting male.
$ pytest
================================================= test session starts ==================================================
platform darwin -- Python 3.7.2, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /Users/cts/Documents/python/tdd
collected 1 item
test_gender.py F [100%]
======================================================= FAILURES =======================================================
____________________________ test_should_return_female_when_the_name_is_from_female_gender _____________________________
def test_should_return_female_when_the_name_is_from_female_gender():
detector = GenderDetector()
expected_gender = detector.run('Elsa')
> assert expected_gender == 'male'
E AssertionError: assert 'female' == 'male'
E - female
E ? --
E + male
test_gender.py:7: AssertionError
================================================== 1 failed in 0.71s ===================================================
If we change the expected gender in line 7 of our test_gender.py
file to expect female, we should see it pass:
assert expected_gender == ‘female’
$ pytest
================================================= test session starts ==================================================
platform darwin -- Python 3.7.2, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /Users/cts/Documents/python/tdd
collected 1 item
test_gender.py . [100%]
================================================== 1 passed in 0.39s ===================================================
Final Thoughts
Thanks to Ana Paula Gomes from Code Like A Girl for explaining how Test Driven Development (TDD) works using unit tests and pytest
.
One of the objectives of the DevNet Associate (DEVASC 200-901) is to describe the concepts of test-driven development.
The takeaway here is creating small unit tests prior to writing your code. Create a small amount of code to fulfill a requirement and run the unit test against that code. pytest
is a library that can help easily test that code.
TDD will help prevent writing duplicate code, excessive code, and I think it helps improve the quality of your code.
At least early on in my DevNet journey, I can incorporate these unit tests with my scripts. I’ll experiment with adding it to future scripts.
pytest -v
will run in verbose mode and show you more details about your test. It also outputs the test in green/red for pass/fail
Leave a Reply