• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Newsletter
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • Wi-Fi 6E
    • CCNP Enterprise Core
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

test driven development

Test Driven Development Concepts

December 9, 2019 By Rowell Leave a Comment

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:

  1. Create a test
  2. Run tests for failure
  3. Write code
  4. Run tests
  5. 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

Primary Sidebar

Recent Posts

  • 6 GHz Frame Captures with EtherScope nXG
  • Manage Cisco Catalyst in the (Meraki) Cloud
  • Q1 2022 Income Report
  • First Look at Ekahau AI Pro – Network Simulator
  • PAN-OS Configuration Management – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • June 2022
  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.