Tool testing guide for new Python developer

Tram Ho

Prologue

Every programming language has a large number of tools for testing the code. And so is Python. However, with the Pandas library, this is not really common. In the article, the author also mentioned two reasons because Pandas can be more difficult to test normal Python code syntax or it is more commonly used by experts than programmers.

However, testing is a phase in software development so it is not advisable to know some tools to ease this process.

Okay, and today’s translation on a fairly new topic is just getting started!!!

Content

Using Pytest

Pytest is a very popular and simple tool for testing code. For some other tools, it requires defining classes for each test pass, which inadvertently makes this a bit more cumbersome. With Pytest it’s simpler and easier.

Install Pytest

To use it, we can use the command:

You can learn more about it at Pytest

Example with Pytest

For example, I want to test the code to calculate the sum of the following two numbers:

I write the code in the file <name>.py (eg operations.py …).

To test the above code, I performed the following steps:

  1. Write the above code in a .py file (eg operations.py )’
  2. Create another .py file to test the above code (eg test_operations.py)
  3. Write the code to test the function sum_a_and_b(a, b) :
    • Import the sum function from the file operations(a, b)
    • Write a function to test the sum function:
    • Run the test file with the terminal:
      • cd into the directory containing the file testing
      • Run the code in the test file with type pytest
    • Check the results

Illustrate successful execution results.

You can learn more about some of the strengths of pytest in this course Python Automatic Pytest

How to write a standard test

Based on the author’s opinion

Beyond the Happy Path

The test version Happy path is the test version 1 which is usually the version we expect first. This is the simplest case and the problem we need the first solver function for. However, we also need cases called the edge cases to evaluate the function 100%.

Illustrate the case of both pass and fail

Testing Principles

For unit tests, it’s a good rule of thumb to remember that tests should always follow the common “arrange/act/assert” structure.

The steps include:

  • arrange : things needed for testing like creating necessary data, preparing in-memory database or mocking API calls…
  • act: function or method to call it
  • assert: expected output.

‼️ Note that, if you want to unit test with user interaction, you can use the following three types: given/when/then

  • Given: what you need to run the test
  • When: interactive
  • Then: expected outcome

Take Your Time Naming Everything Correctly

References:

Thank you

First of all, I would like to thank Mr. Eduardo Motta de Morae – the author of the article. Thank you all for reading my translation. Thanks a lot.

Share the news now

Source : Viblo