我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用ddt.file_data()。
def test_file_data_decorator_with_dict(): """ Test the ``file_data`` method decorator """ def hello(): pass pre_size = len(hello.__dict__) keys = set(hello.__dict__.keys()) data_hello = data("test_data_dict.json")(hello) dh_keys = set(data_hello.__dict__.keys()) post_size = len(data_hello.__dict__) assert_equal(post_size, pre_size + 1) extra_attrs = dh_keys - keys assert_equal(len(extra_attrs), 1) extra_attr = extra_attrs.pop() assert_equal(getattr(data_hello, extra_attr), ("test_data_dict.json",))
def test_file_data_test_names_dict(): """ Test that ``file_data`` creates tests with the correct name Name is the the function name plus the key in the JSON data, when it is parsed as a dictionary. """ tests = set(filter(_is_test, FileDataDummy.__dict__)) tests_dir = os.path.dirname(__file__) test_data_path = os.path.join(tests_dir, 'test_data_dict.json') test_data = json.loads(open(test_data_path).read()) created_tests = set([ "test_something_again_{0}_{1}".format(index + 1, name) for index, name in enumerate(test_data.keys()) ]) assert_equal(tests, created_tests)
def test_load_yaml_without_yaml_support(): """ Test that YAML files are not loaded if YAML is not installed. """ @ddt class NoYAMLInstalledTest(object): @file_data('test_data_dict.yaml') def test_file_data_yaml_dict(self, value): assert_true(has_three_elements(value)) tests = filter(_is_test, NoYAMLInstalledTest.__dict__) obj = NoYAMLInstalledTest() for test in tests: method = getattr(obj, test) assert_raises(ValueError, method)
def test_file_data_test_creation(): """ Test that the ``file_data`` decorator creates two tests """ tests = len(list(filter(_is_test, FileDataDummy.__dict__))) assert_equal(tests, 2)