<aside> 💼

Career Path: Software Engineer, Quality Assurance Engineer, Software Engineer in Test, Software Development Engineer in Test

</aside>

https://www.youtube.com/watch?v=u6QfIXgjwGQ&t=56s

Test automation plays a crucial role in modern software development by ensuring code quality, reducing manual testing efforts, and enabling faster release cycles. By automating repetitive testing tasks, developers can focus on creating new features and improving overall product quality. Moreover, automated tests provide a safety net for code changes, allowing teams to refactor and update their codebase with confidence.

Exercise

In this exercise, we'll cover the fundamental concepts of test automation using Pytest, a popular Python testing framework. We'll start by exploring the basics of writing and running tests, then touch on more advanced features like fixtures and parameterized testing, and then finish with a collaborative coding exercise.

1. Introduction to Pytest

<aside> 💡

Pytest is, in fact, both the tool that I use to automatically test your code on GitHub and the tool that Codewars uses to test your katas. Learning how to read and write it can help you better test your own code, and understand the automated tests as they are written.

</aside>

Pytest is a powerful and flexible testing framework for Python that simplifies the process of writing and running tests. It offers several advantages over other testing frameworks, including a simple syntax, automatic test discovery, and a rich ecosystem of plugins. With pytest, developers can easily create and maintain comprehensive test suites for their Python projects, ensuring code reliability and facilitating continuous integration practices.

To get started with pytest, you'll need to set up your Python environment and install the necessary packages. First, ensure you have Python installed on your system (this should have been completed last week).

Next, we will need to install pytest. Generally, this is accomplished by executing the following command:

pip install pytest

But on many systems (Ubuntu included), Python packages are managed by operating system’s package manager, so you must install pytest using apt instead:

sudo apt install python3-pytest

Once you have pytest installed, you're ready to start writing and running tests for your Python code.

2. Writing Your First Test

Let's explore the basic structure of a pytest test. In pytest, a test is simply a function whose name starts with test_. Here's a simple example:

def test_example():
    assert 1 + 1 == 2

This test function asserts (makes sure) that the code 1 + 1 is equal to 2. If this is True, the test passes; if not, it fails. Here’s a more complete example that tests a function I have created called add() that (simply put) adds two numbers together:

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0

To run the test using pytest, open your terminal, navigate to the directory containing your test file, and execute the following command: