[파이썬] pytest 테스트 리포트 생성

In software development, testing is a crucial aspect of ensuring the quality and correctness of the code. It helps identify bugs, improves software stability, and provides confidence in the overall system. Automated testing frameworks like pytest in Python are widely used for effective and efficient software testing.

One of the important features of pytest is the capability to generate detailed test reports. These reports provide valuable insights into the results of the test suite, making it easier to analyze and debug any issues that may arise during testing. In this blog post, we will explore how to generate pytest test reports in Python.

Installing pytest

Before we begin, make sure you have pytest installed in your Python environment. If you haven’t already installed it, you can do so using pip:

pip install pytest

Running pytest with test reporting

To generate a test report with pytest, we need to run our test suite and specify the desired report format. pytest supports several built-in reporting options, such as JUnit XML, HTML, JSON, and CSV. Let’s see how to generate an HTML test report using pytest.

  1. Create a file named test_report.py and define your test cases using pytest syntax. For example:
import pytest

def test_addition():
    assert 2 + 2 == 4

def test_subtraction():
    assert 5 - 3 == 2

def test_multiplication():
    assert 3 * 4 == 12
  1. Open your terminal or command prompt and navigate to the directory where test_report.py is located.

  2. Run the following command to execute the pytest test suite and generate an HTML report:

pytest --html=report.html

This command runs all the test cases defined in the test_report.py file and generates an HTML test report named report.html. You can choose any name for the report file.

  1. After running the command, pytest will execute the tests and generate the test report file. You can open the report file in a web browser to view the test results, including passed and failed test cases, assertions, and other relevant details.

By default, pytest creates a detailed HTML report that includes information like test execution time, captured output, setup and teardown logs, and more. This report is helpful for understanding the test results and identifying any issues.

Conclusion

Generating test reports using pytest in Python allows us to track and analyze the results of our test suite easily. It provides valuable information about the success and failure of our tests, helping us improve software quality and ensure code stability.

In this blog post, we covered the steps to generate an HTML test report using pytest. However, pytest offers various other reporting options as well, allowing you to choose the format that best suits your requirements.