Junior — Middle
What types of tests can you create to ensure the quality of your code?
sobes.tech AI
Answer from AI
To ensure code quality in Python, you can create several types of tests:
-
Unit tests — check individual functions or methods for correctness in isolation. Usually written using
unittestorpytest. -
Integration tests — verify the interaction of multiple components or modules together.
-
Functional tests — test the application's functionality from the user's perspective, for example through HTTP requests to an API.
-
Regression tests — ensure that new changes do not break existing functionality.
-
Performance tests — measure speed and load.
A simple example of a unit test using pytest:
# my_module.py
def add(a, b):
return a + b
# test_my_module.py
import pytest
from my_module import add
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
This approach helps quickly identify errors and maintain code quality.