1

I have test cases written in Python-Unittest and executing them with PyTest. I want to add PyTest marker to subset of ddt test cases.

But currently able to use PyTest marker for whole test case. Like this -

# cat unittest_marker.py

import unittest, ddt, pytest

@ddt.ddt
class TestCase(unittest.TestCase):
    @pytest.mark.group0
    @ddt.data("a", "b", "c")
    def test_t01(self, val):
        print("t01: {}".format(val))

Collect all test cases marked with group0

# pytest unittest_marker.py -m group0 --collect-only
============================================================================ test session starts =============================================================================
platform linux -- Python 3.6.8, pytest-4.6.3, py-1.8.0, pluggy-0.12.0
rootdir: /root/tests/python/pytest-mark
collected 3 items                                                                                                                                                            
<Module unittest_marker.py>
  <UnitTestCase TestCase>
    <TestCaseFunction test_t01_1_a>
    <TestCaseFunction test_t01_2_b>
    <TestCaseFunction test_t01_3_c>

I want to add marker for test case test_t01_1_b, like in PyTest -

# cat pytest_marker.py 

import pytest

@pytest.mark.group0
@pytest.mark.parametrize(
    ("val"), [("a"), pytest.param("b", marks=pytest.mark.group1), ("c")]
)
def test_t01(val):
    print("t01: {}".format(val))

Collect all test cases marked with group0

# pytest pytest_marker.py -m group0 --collect-only
============================================================================ test session starts =============================================================================
platform linux -- Python 3.6.8, pytest-4.6.3, py-1.8.0, pluggy-0.12.0
rootdir: /root/tests/python/pytest-mark
collected 3 items                                                                                                                                                            
<Module pytest_marker.py>
  <Function test_t01[a]>
  <Function test_t01[b]>
  <Function test_t01[c]>

Collect all test cases marked with group1

# pytest pytest_marker.py -m group1 --collect-only
============================================================================ test session starts =============================================================================
platform linux -- Python 3.6.8, pytest-4.6.3, py-1.8.0, pluggy-0.12.0
rootdir: /root/tests/python/pytest-mark
collected 3 items / 2 deselected / 1 selected                                                                                                                                
<Module pytest_marker.py>
  <Function test_t01[b]>

Is there any way to get PyTest like output using Python-Unittest ddt?

va1bhav
  • 365
  • 1
  • 5
  • 21

0 Answers0