Skip to content

@ifile_data - an iterable @file_data decorator #99

@jasonchester

Description

@jasonchester

tldr; a combination of the @idata and @file_data decorators

Use Case

Add a decorator @ifile_data to pass multiple files as data items into a test function.

  1. Files would be specified by a glob pattern relative to the test files directory.

  2. The name of the test case would be the file.name of each file.

  3. Test generated test names should include the file name.

MyTest_0_some_1234_json
MyTest_1_some_xyz_json

an example of how this operator would be used.

@ddt
class MyTestCase(unittest.TestCase):

  @ifile_data('./data/some_*.json')
  def MyTest(self, some_data):
    self.assertIsNotNone(some_data)

workaround

I am using this as a workaround for now:

from unittest import TestCase, mock
import json
from ddt import ddt, idata
from pathlib import Path


class NamedList(list):
    pass


def get_file_data(glob_pattern):

    result = []

    for file in Path(__file__).parent.glob(glob_pattern):
        with open(file) as reader:
            dataitem = NamedList(json.load(reader))
            setattr(dataitem, '__name__', file.name)
            result.append(dataitem)

    return result


@ddt
class FileDataTests(TestCase):
    @idata(get_file_data('./data/some_*.json'))
    def test_file_data(self, some_data):
        self.assertIsNotNone(some_data)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions