1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
import os
import json
from sys import modules
import pytest
import six
try:
from unittest import mock
except ImportError:
import mock
from ddt import ddt, data, file_data, idata, TestNameFormat
from test.mycode import has_three_elements
class CustomClass:
pass
@ddt
class Dummy(object):
"""
Dummy class to test the data decorator on
"""
@data(1, 2, 3, 4)
def test_something(self, value):
return value
@ddt(testNameFormat=TestNameFormat.DEFAULT)
class DummyTestNameFormatDefault(object):
"""
Dummy class to test the ddt decorator that generates test names using the
default format (index and values).
"""
@data("a", "b", "c", "d")
def test_something(self, value):
return value
@ddt(testNameFormat=TestNameFormat.INDEX_ONLY)
class DummyTestNameFormatIndexOnly(object):
"""
Dummy class to test the ddt decorator that generates test names using only
the index.
"""
@data("a", "b", "c", "d")
def test_something(self, value):
return value
@ddt
class DummyInvalidIdentifier():
"""
Dummy class to test the data decorator receiving values invalid characters
identifiers
"""
@data('32v2 g #Gmw845h$W b53wi.')
def test_data_with_invalid_identifier(self, value):
return value
@ddt
class FileDataDummy(object):
"""
Dummy class to test the file_data decorator on
"""
@file_data("data/test_data_dict.json")
def test_something_again(self, value):
return value
@ddt
class JSONFileDataMissingDummy(object):
"""
Dummy class to test the file_data decorator on when
JSON file is missing
"""
@file_data("data/test_data_dict_missing.json")
def test_something_again(self, value):
return value
@ddt
class YAMLFileDataMissingDummy(object):
"""
Dummy class to test the file_data decorator on when
YAML file is missing
"""
@file_data("data/test_data_dict_missing.yaml")
def test_something_again(self, value):
return value
def test_data_decorator():
"""
Test the ``data`` method decorator
"""
def hello():
pass
pre_size = len(hello.__dict__)
keys = set(hello.__dict__.keys())
data_hello = data(1, 2)(hello)
dh_keys = set(data_hello.__dict__.keys())
post_size = len(data_hello.__dict__)
assert post_size == pre_size + 2
extra_attrs = list(dh_keys - keys)
extra_attrs.sort()
assert len(extra_attrs) == 2
assert getattr(data_hello, extra_attrs[0]) == 1
assert getattr(data_hello, extra_attrs[1]) == (1, 2)
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 post_size == pre_size + 2
extra_attrs = list(dh_keys - keys)
extra_attrs.sort()
assert len(extra_attrs) == 2
assert getattr(data_hello, extra_attrs[0]) == 1
assert getattr(data_hello, extra_attrs[1]) == ("test_data_dict.json",)
def _is_test(x):
return x.startswith('test_')
def test_ddt():
"""
Test the ``ddt`` class decorator
"""
tests = len(list(filter(_is_test, Dummy.__dict__)))
assert tests == 4
def test_ddt_format_test_name_index_only():
"""
Test the ``ddt`` class decorator with ``INDEX_ONLY`` test name format
"""
tests = set(filter(_is_test, DummyTestNameFormatIndexOnly.__dict__))
assert len(tests) == 4
indexes = range(1, 5)
dataSets = ["a", "b", "c", "d"] # @data from DummyTestNameFormatIndexOnly
for i, d in zip(indexes, dataSets):
assert ("test_something_{}".format(i) in tests)
assert not ("test_something_{}_{}".format(i, d) in tests)
def test_ddt_format_test_name_default():
"""
Test the ``ddt`` class decorator with ``DEFAULT`` test name format
"""
tests = set(filter(_is_test, DummyTestNameFormatDefault.__dict__))
assert len(tests) == 4
indexes = range(1, 5)
dataSets = ["a", "b", "c", "d"] # @data from DummyTestNameFormatDefault
for i, d in zip(indexes, dataSets):
assert not ("test_something_{}".format(i) in tests)
assert ("test_something_{}_{}".format(i, d) in tests)
def test_idata_single_argument():
"""Test that the single-argument form of ``idata`` works."""
payload = [5, 12, 13]
@ddt
class Dummy(object):
"""Dummy class to test that the ``idata(iterable)`` decorator works."""
@idata(payload)
def test_something(self, value):
return value
tests = list(filter(_is_test, Dummy.__dict__))
assert len(tests) == len(payload)
expected_tests = [
"test_something_{:1d}_{}".format(i + 1, v) for i, v in enumerate(payload)
]
assert sorted(tests) == sorted(expected_tests)
def test_idata_automatic_zero_padding():
"""
Test that the single-argument form of ``idata`` zero-pads its keys so the
lengths all match
"""
payload = range(15)
@ddt
class Dummy(object):
"""Dummy class to test that the ``idata(iterable)`` decorator works."""
@idata(payload)
def test_something(self, value):
return value
tests = list(filter(_is_test, Dummy.__dict__))
assert len(tests) == len(payload)
expected_tests = [
"test_something_{:02d}_{}".format(i + 1, v) for i, v in enumerate(payload)
]
assert sorted(tests) == sorted(expected_tests)
def test_idata_override_index_len():
"""
Test that overriding ``index_len`` in ``idata`` can allow additional
zero-padding to be added.
"""
payload = [4, 2, 1]
@ddt
class Dummy(object):
@idata(payload, index_len=2)
def test_something(self, value):
return value
tests = list(filter(_is_test, Dummy.__dict__))
assert len(tests) == len(payload)
expected_tests = [
"test_something_{:02d}_{}".format(i + 1, v) for i, v in enumerate(payload)
]
assert sorted(tests) == sorted(expected_tests)
def test_idata_consumable_iterator():
"""
Test that using ``idata`` with a consumable iterator still generates the
expected tests.
"""
payload = [51, 78, 2]
def consumable_iterator():
# Not using `yield from` for Python 2.7.
for i in payload:
yield i
@ddt
class Dummy(object):
@idata(consumable_iterator())
def test_something(self, value):
return value
tests = list(filter(_is_test, Dummy.__dict__))
expected_tests = [
"test_something_{:1d}_{}".format(i + 1, v) for i, v in enumerate(payload)
]
assert sorted(tests) == sorted(expected_tests)
def test_file_data_test_creation():
"""
Test that the ``file_data`` decorator creates two tests
"""
tests = len(list(filter(_is_test, FileDataDummy.__dict__)))
assert tests == 2
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, 'data/test_data_dict.json')
test_data = json.loads(open(test_data_path).read())
index_len = len(str(len(test_data)))
created_tests = set([
"test_something_again_{0:0{2}}_{1}".format(index + 1, name, index_len)
for index, name in enumerate(test_data.keys())
])
assert tests == created_tests
def test_feed_data_data():
"""
Test that data is fed to the decorated tests
"""
tests = filter(_is_test, Dummy.__dict__)
values = []
obj = Dummy()
for test in tests:
method = getattr(obj, test)
values.append(method())
assert set(values) == set([1, 2, 3, 4])
def test_feed_data_file_data():
"""
Test that data is fed to the decorated tests from a file
"""
tests = filter(_is_test, FileDataDummy.__dict__)
values = []
obj = FileDataDummy()
for test in tests:
method = getattr(obj, test)
values.extend(method())
assert set(values) == set([10, 12, 15, 15, 12, 50])
def test_feed_data_file_data_missing_json():
"""
Test that a ValueError is raised when JSON file is missing
"""
tests = filter(_is_test, JSONFileDataMissingDummy.__dict__)
obj = JSONFileDataMissingDummy()
for test in tests:
method = getattr(obj, test)
with pytest.raises(ValueError):
method()
def test_feed_data_file_data_missing_yaml():
"""
Test that a ValueError is raised when YAML file is missing
"""
tests = filter(_is_test, YAMLFileDataMissingDummy.__dict__)
obj = YAMLFileDataMissingDummy()
for test in tests:
method = getattr(obj, test)
with pytest.raises(ValueError):
method()
def test_ddt_data_name_attribute():
"""
Test the ``__name__`` attribute handling of ``data`` items with ``ddt``
"""
def hello():
pass
class Myint(int):
pass
class Mytest(object):
pass
d1 = Myint(1)
d1.__name__ = 'data1'
d2 = Myint(2)
data_hello = data(d1, d2)(hello)
setattr(Mytest, 'test_hello', data_hello)
ddt_mytest = ddt(Mytest)
assert getattr(ddt_mytest, 'test_hello_1_data1')
assert getattr(ddt_mytest, 'test_hello_2_2')
def test_ddt_data_doc_attribute():
"""
Test the ``__doc__`` attribute handling of ``data`` items with ``ddt``
"""
def func_w_doc():
"""testFunctionDocstring {6}
:param: None
:return: None
"""
pass
def func_wo_doc():
pass
class Myint(int):
pass
class Mytest(object):
pass
d1 = Myint(1)
d1.__name__ = 'case1'
d1.__doc__ = """docstring1"""
d2 = Myint(2)
d2.__name__ = 'case2'
data_hello = data(d1, d2, {'test': True})(func_w_doc)
data_hello2 = data(d1, d2, {'test': True})(func_wo_doc)
setattr(Mytest, 'first_test', data_hello)
setattr(Mytest, 'second_test', data_hello2)
ddt_mytest = ddt(Mytest)
assert getattr(
getattr(ddt_mytest, 'first_test_1_case1'), '__doc__'
) == d1.__doc__
assert getattr(
getattr(ddt_mytest, 'first_test_2_case2'), '__doc__'
) == func_w_doc.__doc__
assert getattr(
getattr(ddt_mytest, 'first_test_3'), '__doc__'
) == func_w_doc.__doc__
assert getattr(
getattr(ddt_mytest, 'second_test_1_case1'), '__doc__'
) == d1.__doc__
assert getattr(
getattr(ddt_mytest, 'second_test_2_case2'), '__doc__'
) is None
assert getattr(getattr(ddt_mytest, 'second_test_3'), '__doc__') is None
def test_ddt_data_unicode():
"""
Test that unicode strings are converted to function names correctly
"""
# We test unicode support separately for python 2 and 3
if six.PY2:
@ddt
class Mytest(object):
@data(u'ascii', u'non-ascii-\N{SNOWMAN}', {u'\N{SNOWMAN}': 'data'})
def test_hello(self, val):
pass
assert getattr(Mytest, 'test_hello_1_ascii') is not None
assert getattr(Mytest, 'test_hello_2_non_ascii__u2603') is not None
assert getattr(Mytest, 'test_hello_3') is not None
elif six.PY3:
@ddt
class Mytest(object):
@data('ascii', 'non-ascii-\N{SNOWMAN}', {'\N{SNOWMAN}': 'data'})
def test_hello(self, val):
pass
assert getattr(Mytest, 'test_hello_1_ascii') is not None
assert getattr(Mytest, 'test_hello_2_non_ascii__') is not None
assert getattr(Mytest, 'test_hello_3') is not None
def test_ddt_data_object():
"""
Test not using value if non-trivial arguments
"""
@ddt
class Mytest(object):
@data(object())
def test_object(self, val):
pass
assert getattr(Mytest, 'test_object_1') is not None
def test_feed_data_with_invalid_identifier():
"""
Test that data is fed to the decorated tests
"""
tests = list(filter(_is_test, DummyInvalidIdentifier.__dict__))
assert len(tests) == 1
obj = DummyInvalidIdentifier()
method = getattr(obj, tests[0])
assert (
method.__name__ ==
'test_data_with_invalid_identifier_1_32v2_g__Gmw845h_W_b53wi_'
)
assert method() == '32v2 g #Gmw845h$W b53wi.'
@mock.patch('ddt._have_yaml', False)
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('data/test_data_dict.yaml')
def test_file_data_yaml_dict(self, value):
assert has_three_elements(value)
tests = filter(_is_test, NoYAMLInstalledTest.__dict__)
obj = NoYAMLInstalledTest()
for test in tests:
method = getattr(obj, test)
with pytest.raises(ValueError):
method()
def test_load_yaml_with_python_tag():
"""
Test that YAML files containing python tags throw no exception if an
loader allowing python tags is passed.
"""
from yaml import UnsafeLoader
from yaml.constructor import ConstructorError
def str_to_type(class_name):
return getattr(modules[__name__], class_name)
try:
@ddt
class YamlDefaultLoaderTest(object):
@file_data('data/test_functional_custom_tags.yaml')
def test_cls_is_instance(self, cls, expected):
assert isinstance(cls, str_to_type(expected))
except Exception as e:
if not isinstance(e, ConstructorError):
raise AssertionError()
@ddt
class YamlUnsafeLoaderTest(object):
@file_data('data/test_functional_custom_tags.yaml', UnsafeLoader)
def test_cls_is_instance(self, instance, expected):
assert isinstance(instance, str_to_type(expected))
tests = list(filter(_is_test, YamlUnsafeLoaderTest.__dict__))
obj = YamlUnsafeLoaderTest()
if not tests:
raise AssertionError('No tests have been found.')
for test in tests:
method = getattr(obj, test)
method()
|