2

For a repetitive unit test, I have several test cases in a Yaml-File (resources/test_cases.yml), which looks like this:

scenario_1:
  test_cases:
    expected:
      - 1
      - 1
      - 1
      - 1

scenario_2:
  test_cases:
    expected:
      - 1
      - 1
      - 1

My unit test looks like this:

import ddt
from unittest import TestCase

@ddt.ddt
class MyTestCase(TestCase):
    @ddt.file_data("resources/test_cases.yml")
    def test_data(self, test_cases):
        for test_case in test_cases:
            with self.subTest():
                expected_result = test_case["expected"]
                self.assertEqual(expected_result, 1)
        # after that, I would like to ensure that 2 scenarios where tested
        # and 7 tests ran in total

as specified in the code comments, I'd like to ensure that every test in the yaml file ran (and none was skipped, for whatever reason).

Question

Is there a way to find out:

  • How many test cases were inserted through the decorator @ddt.file_data("resources/test_cases.yml") (in the example: 2)
  • How many subtests were actually run for one test case (in scenario_1: 4, in scenario_2: 3)
  • How many tests actually ran in total (7)

Of course I could introduce a counter-variable, but that seems cumbersome and I would assume that theres a way to find this out. I know there is method countTestCases() but as stated in the docs, it always yields 1, no matter where I put it.

I already found two related questions (first and second) but it both do not make use of subtests and an external file. Also, it seems quite cumbersome and I was thinking that my problem should have quite straightforward solution

bk_
  • 751
  • 1
  • 8
  • 27

0 Answers0