Automatic creation of test documentation based on autotests using Python and QASE.io

First of all, in the fixtures directory, we will create a new qase_fixtures.py module.

Add the following code to the qase_fixtures.py module:

import pytest

import config
import qase
import api


@pytest.fixture(autouse=config.qase.IS_DOC_UPDATE_NEEDED)
def update_docs(request) -> None:
    case_id_marker = request.node.get_closest_marker('case_id')
    if config.qase.TOKEN and len(case_id_marker.args) > 0:
        qase.docs.create_database(request)
    yield
    if config.qase.TOKEN and len(case_id_marker.args) > 0:
        api.qase.update_case(case_id_marker.args[0], qase.docs.read())
        qase.docs.delete_database()

It’s important to note that after pasting the code into the module, PyCharm will show errors that we haven’t written the config.qase and qase.api modules yet. That’s the way it should be. We will write them soon. For now, let’s look at what the fixture code does.

Line 8. Denote that this is a fixture. And we use the autouse parameter. It can take True/False values. Since we will not need to generate documentation every time we run pytest, we will set up config.qase so that it can accept the value of the IS_DOC_UPDATE_NEEDED variable from a command in the terminal. Thus, by setting the value of this variable as True or False in the terminal, we can control the behavior of the documentation (whether we generate it or not).

Line 9. We pass request as a function parameter. It will allow us to access the values ​​in the ‘@pytest.marl.case_id(1)’ attribute that we add to each autotest.

Line 10. Getting the parameter value from the case_id attribute.

Line 11 and 14. To send a request to the QASE API, you will need the Token that we previously obtained from the QASE.io website. But the Token must be kept safe. We will not store it in the project code. Therefore, Token will work similarly with the IS_DOC_UPDATE_NEEDED variable and we will get its value from the command in the terminal. In the next step, we will create the config.qase module, where we will describe the behavior of all these variables.

Line 12. Use the create_database method, which creates a json file to store information about the steps.

Line 13. Yield itself has two important implications. First, it can work as return if you specify something after yield. Secondly, it is a separator. What is written before yield will be executed before the autotest. What is written after yield will be executed after the autotest. Therefore, before yield, we have a file creation, and after it, sending values ​​​​to QASE.io and deleting the file.

Line 15. Request to the QASE API. In this query, we will update the steps in the case. We will create a request to the QASE API very soon.

In order for pytest to find this fixture in the future, we need to add it to the conftest.py file:

pytest_plugins = [
    'fixtures.page',
    'fixtures.qase_fixtures'
]

So now we need to create config.qase and api.qase_api. Let’s take care of this.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *