Testing is indispensable and crucial when developing an API for your application. It ensures that all functionalities works correctly and prevents unexpected errors. In Python, we can combine Pytest for functional testing and the Flake8 is an analysis tool to check the quality of a source code. Today, we will explore how to set up and use these tools effectively for testing an API.

1. What is Pytest?

Pytest is a flexible testing framework that supports fixtures, written in Python, and used for parameterized testing and plugins. It is ideal for API testing, capable of handling both basic and complex test cases.

2. Why write unit tests?

  • Ensure that the returned results are as expected, confirming the correctness of the API.
  • Help detect and identify parts affected by new updates, making maintenance easier.
  • Help discover and fix errors early in the development phase, reducing the risk of serious issues in the production environment.
  • Test individual parts of the API to ensure they function correctly and reliably.

3. Environment Setup

3.1. Install pytest: pip install pytest

3.2. Install flake8: pip install flake8

3.3. Install requests library: pip install requests

3.3. Install flask: pip install flask

3.4. Verify installation:

pytest --version
flake8 --version

4. How to Implement Pytest

4.1. Create an API file: app.py

4.2. Create test cases:

Test Case ID Description Steps Type Expected Result Date
TC001 Call API successful Request to the URL: http://127.0.0.1:5000/division Normal case
status_code: 200
message: "Successful"
result: 1
dd/mm/yyyy
TC002 Missing parameter "value1" Request to the URL: http://127.0.0.1:5000/division Error case
status_code: 400
error: Missing parameters
dd/mm/yyyy
TC003 Missing parameter "value2" Request to the URL: http://127.0.0.1:5000/division Error case
status_code: 400
error: Missing parameters
dd/mm/yyyy
TC004 Parameter value 2 is zero Request to the URL: http://127.0.0.1:5000/division Error case
status_code: 400
error: Invalid parameters
dd/mm/yyyy
more... ... ... ... ... ...

4.3. Create test code:

Create a test file and write your test cases. Your test file should start with test_ or end with _test.py. Here’s an example of a test file: test_app.py

4.4. Execute tests

Open a terminal and run the API: python app.py

Open another terminal and run Pytest: pytest test_app.py -v

4.5. Results

Pytest will display the results of the test cases in the terminal

4.5.1 Fail

4.5.2 Passed all test cases

5. What is flake8 and How to Implement It?

5.1. What is flake8?

Flake8 is a Python linting tool that helps you check your code for common issues and ensures it follows PEP 8 (Python's style guide).

Flake8 checks for:

  • PEP 8 compliance (Python's style guide).
  • Common bugs or code smells.
  • Unused variables or imports.

By integrating flake8, you can ensure that your code is clean, maintainable, and adheres to industry standards.

5.2. Run Flake8 to check for issues in the source code: flake8 app.py

5.3. Result:

Fail

When an error occurs, you can refer to the rules of Flake8 at Flake8 Rules (https://www.flake8rules.com/) to fix the error. This site provides a detailed list of rules, helping you to easily understand and correct issues in the source code according to PEP 8 standards.

Passed

Conclusion

Combining Pytest and Flake8 streamlines code quality assurance. Pytest validates functionality, while Flake8 enforces coding standards, ensuring your code is both robust and well-structured.

Reference

  1. https://docs.pytest.org/en/
  2. https://flake8.pycqa.org/en/latest/
  3. https://www.flake8rules.com/
  4. https://dev.to/coderpad/a-guide-to-database-unit-testing-with-pytest-and-sqlalchemy-1i96