reportportal / agent-python-pytest

Framework integration with PyTest
Apache License 2.0
94 stars 103 forks source link

Any option to give custom name to test case . #344

Closed ChetanKolhe closed 1 year ago

ChetanKolhe commented 1 year ago

Hi All, I am looking for option to give custom name to test case , instead of picking from function name . And also looking for option to group the test cases according to feature or suite .

Like following way .

Suite 1 : Test 1, Test 2 Suite 2 : Test 1, Test 2

Similar feature already available in Testng reportportal by overriding listener .

HardNorth commented 1 year ago

@ChetanKolhe, there is no such option, unless pytest implement such official feature.

rplevka commented 1 year ago

@HardNorth , Sure, there is a way: we can agree on a docstring token, e.g. :rp_custom_name:, so the test will look e.g. like:

def test_mytest():
    """
    one of my random tests

    :rp_custom_name: foo_bar
    """
    pass

and append some code to process it here (it could be possible to override any param this way) something like:

import re
...
    def _build_start_step_rq(self, leaf):
        docs_match = re.search(r':rp_custom_name:\s(?P<name>[^$\s]+)', leaf.obj.__doc__) or {}
        payload = {
            'attributes': leaf.get('attributes', None),
            'name': docs_match.get('name') or self._get_item_name(leaf['name']),
            'description': self._get_item_description(leaf['item']),
            'start_time': timestamp(),
            'item_type': 'STEP',
            'code_ref': leaf.get('code_ref', None),
            'parameters': leaf.get('parameters', None),
            'parent_item_id': self._lock(leaf['parent'],
                                         lambda p: p['item_id']),
            'test_case_id': leaf.get('test_case_id', None)
        }
        return payload

^ I typed that from a top of my head, so it might need some tweaking