File size: 32,913 Bytes
16dcd38 |
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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import random
import time
import unittest
from geco_data_generator import attrgenfunct
random.seed(42)
# Define the number of tests to be done for the functionality tests
num_tests = 10000
# Define argument test cases here
test_argument_data_dict = {
('attrgenfunct', 'n/a', 'generate_uniform_value'): {
'min_val': [
[
[-10, 10, 'int'],
[10, 110, 'float1'],
[1110.0, 11011.9, 'float2'],
[-1110.0, -11.9, 'float9'],
[-51110, 1247, 'float7'],
],
[
['-10', 10, 'int'],
[None, 10, 'float1'],
[{}, 11011.9, 'float2'],
["-1110.0", -11.9, 'float9'],
['int', 1247, 'float7'],
],
],
'max_val': [
[
[-10, 10, 'int'],
[10, 110, 'float1'],
[1110.0, 11011.9, 'float2'],
[-1110.0, -11.9, 'float9'],
[-51110, 1247, 'float7'],
],
[
[-10, '10', 'int'],
[10, None, 'float1'],
[1110.0, {}, 'float2'],
[-1110.0, "-11.9", 'float9'],
[-51110, 'int', 'float7'],
],
],
'val_type': [
[
[-10, 10, 'int'],
[10, 110, 'float1'],
[1110.0, 11011.9, 'float2'],
[1.0, 9.9, 'float3'],
[1.0, 2.9, 'float4'],
[1.0, 11.9, 'float5'],
[1.0, 9.9, 'float6'],
[1.0, 2.9, 'float7'],
[1.0, 11.9, 'float8'],
[-1110.0, -11.9, 'float9'],
],
[
[-10, 10, 'int2'],
[10, 110, 'float0'],
[1110.0, 11011.9, 'float-'],
[1.0, 9.9, 'test'],
[1.0, 2.9, {}],
[1.0, 11.9, ''],
[1.0, 9.9, 42],
[1.0, 2.9, -42.42],
[1.0, 11.9, []],
],
],
},
#
('attrgenfunct', 'n/a', 'generate_uniform_age'): {
'min_val': [
[[0, 120], [10, 100], [25.0, 76.9], [55, 56], [55.0, 57.9]],
[[-1, 120], [210, 100], ['25.0', 76.9], [{}, 56], [-55.0, 57.9]],
],
'max_val': [
[[0, 120], [10, 100], [25.0, 76.9], [55, 56], [55.0, 57.9]],
[[0, 140], [10, -100], [25.0, '76.9'], [55, {}], [55.0, -57.9]],
],
},
#
('attrgenfunct', 'n/a', 'generate_normal_value'): {
'mu': [
[
[1.0, 1.0, -10, 10, 'int'],
[-5, 25, -10, 110, 'float1'],
[100.42, 2000, -1110.0, 11011.9, 'float2'],
[24.24, 5.5, None, 30.0, 'float9'],
[24.24, 5.5, 10.0, None, 'float7'],
],
[
['1.0', 1.0, -10, 10, 'int'],
[-55, 25, -10, 110, 'float1'],
[255, 25, -10, 110, 'float1'],
[None, 2000, -1110.0, 11011.9, 'float2'],
[[], 5.5, None, 30.0, 'float9'],
[{}, 5.5, 10.0, None, 'float7'],
],
],
'sigma': [
[
[1.0, 1.0, -10, 10, 'int'],
[-5, 25, -10, 110, 'float1'],
[100.42, 2000, -1110.0, 11011.9, 'float2'],
[24.24, 5.5, None, 30.0, 'float9'],
[24.24, 5.5, 10.0, None, 'float7'],
],
[
[1.0, '1.0', -10, 10, 'int'],
[-5, -25, -10, 110, 'float1'],
[100.42, None, -1110.0, 11011.9, 'float2'],
[24.24, {}, None, 30.0, 'float9'],
[24.24, [], 10.0, None, 'float7'],
],
],
'min_val': [
[
[1.0, 1.0, -10, 10, 'int'],
[-5, 25, -10, 110, 'float1'],
[100.42, 2000, -1110.0, 11011.9, 'float2'],
[24.24, 5.5, None, 30.0, 'float9'],
[24.24, 5.5, 10.0, None, 'float7'],
],
[
[1.0, 1.0, '-10', 10, 'int'],
[-5, 25, 120, 110, 'float1'],
[100.42, 2000, {}, 11011.9, 'float2'],
[24.24, 5.5, 'None', 30.0, 'float9'],
[24.24, 5.5, 120.0, None, 'float7'],
],
],
'max_val': [
[
[1.0, 1.0, -10, 10, 'int'],
[-5, 25, -10, 110, 'float1'],
[100.42, 2000, -1110.0, 11011.9, 'float2'],
[24.24, 5.5, None, 30.0, 'float9'],
[24.24, 5.5, 10.0, None, 'float7'],
],
[
[1.0, 1.0, -10, '10', 'int'],
[-5, 25, -10, -110, 'float1'],
[100.42, 2000, -1110.0, {}, 'float2'],
[24.24, 5.5, None, -30.0, 'float9'],
[24.24, 5.5, 10.0, [], 'float7'],
],
],
'val_type': [
[
[1.0, 1.0, -10, 10, 'int'],
[-5, 25, -10, 110, 'float1'],
[100.42, 2000, -1110.0, 11011.9, 'float2'],
[24.24, 5.5, None, 30.0, 'float9'],
[24.24, 5.5, 10.0, None, 'float7'],
],
[
[1.0, 1.0, -10, 10, 'int2'],
[-5, 25, -10, 110, 'float21'],
[100.42, 2000, -1110.0, 11011.9, None],
[24.24, 5.5, None, 30.0, 42.42],
[24.24, 5.5, 10.0, None, {}],
],
],
},
#
('attrgenfunct', 'n/a', 'generate_normal_age'): {
'mu': [
[
[51.0, 1.0, 0, 110],
[45, 25, 4, 110],
[50.42, 50, 5, 77],
[24.24, 5.5, 1, 50.0],
[24.24, 5.5, 10.0, 99],
],
[
['51.0', 1.0, 0, 110],
[-45, 25, 4, 110],
[223, 25, 4, 110],
[None, 50, 5, 77],
[30, 20, 40, 110],
[70, 20, 10, 60],
[{}, 5.5, 1, 50.0],
['', 5.5, 10.0, 99],
],
],
'sigma': [
[
[51.0, 1.0, 0, 110],
[45, 25, 4, 110],
[50.42, 50, 5, 77],
[24.24, 5.5, 1, 50.0],
[24.24, 5.5, 10.0, 99],
],
[
[51.0, -1.0, 0, 110],
[45, '25', 4, 110],
[50.42, None, 5, 77],
[24.24, {}, 1, 50.0],
[24.24, [], 10.0, 99],
],
],
'min_val': [
[
[51.0, 1.0, 0, 110],
[45, 25, 4, 110],
[50.42, 50, 5, 77],
[24.24, 5.5, 1, 50.0],
[24.24, 5.5, 10.0, 99],
],
[
[51.0, 1.0, -10, 110],
[45, 25, 134, 110],
[50.42, 50, 'None', 77],
[24.24, 5.5, {}, 50.0],
[24.24, 5.5, [], 99],
],
],
'max_val': [
[
[51.0, 1.0, 0, 110],
[45, 25, 4, 110],
[50.42, 50, 5, 77],
[24.24, 5.5, 1, 50.0],
[24.24, 5.5, 10.0, 99],
],
[
[51.0, 1.0, 0, '110'],
[45, 25, 4, -110],
[50.42, 50, 5, 'None'],
[24.24, 5.5, 1, {}],
[24.24, 5.5, 10.0, []],
],
],
},
# add tests for generate_normal_value, generate_normal_age
}
# =============================================================================
class TestCase(unittest.TestCase):
# Initialise test case - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
def setUp(self):
pass # Nothing to initialize
# Clean up test case - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
def tearDown(self):
pass # Nothing to clean up
# ---------------------------------------------------------------------------
# Start test cases
def testArguments(self, test_data):
"""Test if a function or method can be called or initialised correctly with
different values for their input arguments (parameters).
The argument 'test_data' must be a dictionary with the following
structure:
- Keys are tuples consisting of three strings:
(module_name, class_name, function_or_method_name)
- Values are dictionaries where the keys are the names of the input
argument that is being tested, and the values of these dictionaries
are a list that contains two lists. The first list contains valid
input arguments ('normal' argument tests) that should pass the test,
while the second list contains illegal input arguments ('exception'
argument tests) that should raise an exception.
The lists of test cases are itself lists, each containing a number of
input argument values, as many as are expected by the function or
method that is being tested.
This function returns a list containing the test results, where each
list element is a string with comma separated values (CSV) which are to
be written into the testing log file.
"""
test_res_list = [] # The test results, one element per element in the test
# data dictionary
for (test_method_names, test_method_data) in test_data.iteritems():
test_method_name = test_method_names[2]
print('Testing arguments for method/function:', test_method_name)
for argument_name in test_method_data:
print(' Testing input argument:', argument_name)
norm_test_data = test_method_data[argument_name][0]
exce_test_data = test_method_data[argument_name][1]
print(' Normal test cases: ', norm_test_data)
print(' Exception test cases:', exce_test_data)
# Conduct normal tests - - - - - - - - - - - - - - - - - - - - - - - -
#
num_norm_test_cases = len(norm_test_data)
num_norm_test_passed = 0
num_norm_test_failed = 0
norm_failed_desc_str = ''
for test_input in norm_test_data:
passed = True # Assume the test will pass :-)
if len(test_input) == 0: # Method has no input argument
try:
getattr(attrgenfunct, test_method_name)()
except:
passed = False
elif len(test_input) == 1: # Method has one input argument
try:
getattr(attrgenfunct, test_method_name)(test_input[0])
except:
passed = False
elif len(test_input) == 2: # Method has two input arguments
try:
getattr(attrgenfunct, test_method_name)(
test_input[0], test_input[1]
)
except:
passed = False
elif len(test_input) == 3: # Method has three input arguments
try:
getattr(attrgenfunct, test_method_name)(
test_input[0], test_input[1], test_input[2]
)
except:
passed = False
elif len(test_input) == 4: # Method has four input arguments
try:
getattr(attrgenfunct, test_method_name)(
test_input[0], test_input[1], test_input[2], test_input[3]
)
except:
passed = False
elif len(test_input) == 5: # Method has five input arguments
try:
getattr(attrgenfunct, test_method_name)(
test_input[0],
test_input[1],
test_input[2],
test_input[3],
test_input[4],
)
except:
passed = False
else:
raise Exception('Illegal number of input arguments')
# Now process test results
#
if passed == False:
num_norm_test_failed += 1
norm_failed_desc_str += 'Failed test for input ' + "'%s'; " % (
str(test_input)
)
else:
num_norm_test_passed += 1
assert num_norm_test_failed + num_norm_test_passed == num_norm_test_cases
norm_test_result_str = (
test_method_names[0]
+ ','
+ test_method_names[1]
+ ','
+ test_method_names[2]
+ ','
+ argument_name
+ ',normal,'
+ '%d,' % (num_norm_test_cases)
)
if num_norm_test_failed == 0:
norm_test_result_str += 'all tests passed'
else:
norm_test_result_str += '%d tests failed,' % (num_norm_test_failed)
norm_test_result_str += '"' + norm_failed_desc_str[:-2] + '"'
test_res_list.append(norm_test_result_str)
# Conduct exception tests - - - - - - - - - - - - - - - - - - - - - - -
#
num_exce_test_cases = len(exce_test_data)
num_exce_test_passed = 0
num_exce_test_failed = 0
exce_failed_desc_str = ''
for test_input in exce_test_data:
passed = True # Assume the test will pass (i.e. raise an exception)
if len(test_input) == 0: # Method has no input argument
try:
self.assertRaises(
Exception, getattr(attrgenfunct, test_method_name)
)
except:
passed = False
elif len(test_input) == 1: # Method has one input argument
try:
self.assertRaises(
Exception,
getattr(attrgenfunct, test_method_name),
test_input[0],
)
except:
passed = False
elif len(test_input) == 2: # Method has two input arguments
try:
self.assertRaises(
Exception,
getattr(attrgenfunct, test_method_name),
test_input[0],
test_input[1],
)
except:
passed = False
elif len(test_input) == 3: # Method has three input arguments
try:
self.assertRaises(
Exception,
getattr(attrgenfunct, test_method_name),
test_input[0],
test_input[1],
test_input[2],
)
except:
passed = False
elif len(test_input) == 4: # Method has four input arguments
try:
self.assertRaises(
Exception,
getattr(attrgenfunct, test_method_name),
test_input[0],
test_input[1],
test_input[2],
test_input[3],
)
except:
passed = False
elif len(test_input) == 5: # Method has five input arguments
try:
self.assertRaises(
Exception,
getattr(attrgenfunct, test_method_name),
test_input[0],
test_input[1],
test_input[2],
test_input[3],
test_input[4],
)
except:
passed = False
else:
raise Exception('Illegal number of input arguments')
# Now process test results
#
if passed == False:
num_exce_test_failed += 1
exce_failed_desc_str += 'Failed test for input ' + "'%s'; " % (
str(test_input)
)
else:
num_exce_test_passed += 1
assert num_exce_test_failed + num_exce_test_passed == num_exce_test_cases
exce_test_result_str = (
test_method_names[0]
+ ','
+ test_method_names[1]
+ ','
+ test_method_names[2]
+ ','
+ argument_name
+ ',exception,'
+ '%d,' % (num_exce_test_cases)
)
if num_exce_test_failed == 0:
exce_test_result_str += 'all tests passed'
else:
exce_test_result_str += '%d tests failed,' % (num_exce_test_failed)
exce_test_result_str += '"' + exce_failed_desc_str[:-2] + '"'
test_res_list.append(exce_test_result_str)
test_res_list.append('') # Empty line between tests of methods
return test_res_list
# ---------------------------------------------------------------------------
def testFunct_generate_phone_number_australia(self, num_tests):
"""Test the functionality of 'generate_phone_number_australia', making
sure this function returns strings of consisting only of digits, with
the first digit being 0, and two whitespaces at specific positions.
"""
print('Testing functionality of "generate_phone_number_australia"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
oz_phone_num = attrgenfunct.generate_phone_number_australia()
passed = True
if len(oz_phone_num) != 12:
passed = False
if oz_phone_num[0] != '0':
passed = False
if oz_phone_num[2] != ' ':
passed = False
if oz_phone_num[7] != ' ':
passed = False
oz_phone_num_no_space = oz_phone_num.replace(' ', '') # Remove spaces
if not oz_phone_num_no_space.isdigit():
passed = False
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == num_tests
test_result_str = (
'attrgenfunct,n/a,generate_phone_number_australia,'
+ 'n/a,funct,%d,' % (num_tests)
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# ---------------------------------------------------------------------------
def testFunct_generate_credit_card_number(self, num_tests):
"""Test the functionality of 'generate_credit_card_number', making sure
this function returns strings of consisting only of four groups of
digits, eac hwith 4 digits, and three whitespaces at specific positions.
"""
print('Testing functionality of "generate_credit_card_number"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
cc_num = attrgenfunct.generate_credit_card_number()
passed = True
if len(cc_num) != 19:
passed = False
if cc_num[4] != ' ':
passed = False
if cc_num[9] != ' ':
passed = False
if cc_num[14] != ' ':
passed = False
cc_num_no_space = cc_num.replace(' ', '') # Remove spaces
if not cc_num_no_space.isdigit():
passed = False
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == num_tests
test_result_str = (
'attrgenfunct,n/a,generate_credit_card_number,' + 'n/a,funct,%d,' % (num_tests)
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# ---------------------------------------------------------------------------
def testFunct_generate_uniform_value(self, num_tests):
"""Test the functionality of 'generate_uniform_value', making sure
this function returns a string according to the given value type in
the range between the minimum and maximum value specified.
"""
print('Testing functionality of "generate_uniform_value"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
for val_type in [
'int',
'float1',
'float2',
'float3',
'float4',
'float5',
'float6',
'float7',
'float8',
'float9',
]:
for min_val in [-100.0, -42, 0, 10, 100.0]:
for max_val in [200.0, 242, 10000.01]:
norm_val = attrgenfunct.generate_uniform_value(
min_val, max_val, val_type
)
passed = True
if float(norm_val) < min_val:
passed = False
if float(norm_val) > max_val:
passed = False
if (val_type == 'int') and ('.' in norm_val):
passed = False
if val_type != 'int':
num_digit = int(val_type[-1])
norm_val_list = norm_val.split('.')
if len(norm_val_list[1]) > num_digit:
passed = (
False # Only larger, because for example 100.0 will
)
# only return with 1 digit after comma
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == (15 * 10 * num_tests)
test_result_str = 'attrgenfunct,n/a,generate_uniform_value,' + 'n/a,funct,%d,' % (
15 * 10 * num_tests
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# ---------------------------------------------------------------------------
def testFunct_generate_uniform_age(self, num_tests):
"""Test the functionality of 'generate_uniform_age', making sure this
function returns a string with an integer value between 0 and 130.
"""
print('Testing functionality of "generate_uniform_age"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
for (min_val, max_val) in [(0, 10), (0, 120), (0, 45), (42, 48), (40, 120)]:
age_val = attrgenfunct.generate_uniform_age(min_val, max_val)
passed = True
if float(age_val) < min_val:
passed = False
if float(age_val) > max_val:
passed = False
if '.' in age_val:
passed = False
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == num_tests * 5
test_result_str = 'attrgenfunct,n/a,generate_uniform_age,' + 'n/a,funct,%d,' % (
num_tests * 5
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# ---------------------------------------------------------------------------
def testFunct_generate_normal_value(self, num_tests):
"""Test the functionality of 'generate_normal_value', making sure this
function returns a string according to the given value type in the
range between the minimum and maximum value specified.
"""
print('Testing functionality of "generate_normal_value"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
for val_type in [
'int',
'float1',
'float2',
'float3',
'float4',
'float5',
'float6',
'float7',
'float8',
'float9',
]:
for (mu, sigma, min_val, max_val) in [
(0, 1, -10, 10),
(0, 1, -1, 1),
(-100.5, 123.45, -1010.7, -10.11),
(12345.87, 54875.1, -400532, 96344),
(0, 1, None, 10),
(0, 1, None, 1),
(-100.5, 123.45, None, -10.11),
(12345.87, 54875.1, None, 96344),
(0, 1, -10, None),
(0, 1, -1, None),
(-100.5, 123.45, -1010.7, None),
(12345.87, 54875.1, -400532, None),
(0, 1, None, None),
(0, 1, None, None),
(-100.5, 123.45, None, None),
(12345.87, 54875.1, None, None),
]:
norm_val = attrgenfunct.generate_normal_value(
mu, sigma, min_val, max_val, val_type
)
passed = True
if (min_val != None) and (float(norm_val) < min_val):
print('1:', norm_val, min_val)
passed = False
if (max_val != None) and (float(norm_val) > max_val):
print('2:', norm_val, max_val)
passed = False
if (val_type == 'int') and ('.' in norm_val):
print('3:', norm_val)
passed = False
if val_type != 'int':
num_digit = int(val_type[-1])
norm_val_list = norm_val.split('.')
if len(norm_val_list[1]) > num_digit:
print('4:', norm_val, val_type)
passed = False # Only larger, because for example 100.0 will
# only return with 1 digit after comma
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == (16 * 10 * num_tests)
test_result_str = 'attrgenfunct,n/a,generate_normal_value,' + 'n/a,funct,%d,' % (
16 * 10 * num_tests
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# ---------------------------------------------------------------------------
def testFunct_generate_normal_age(self, num_tests):
"""Test the functionality of 'generate_normal_age', making sure this
function returns a string with an integer value between 0 and 130.
"""
print('Testing functionality of "generate_normal_age"')
num_passed = 0
num_failed = 0
for i in range(num_tests):
for (mu, sigma, min_val, max_val) in [
(50, 100, 0, 130),
(25, 20, 5, 55),
(65, 10, 60, 130),
(85, 20, 0, 95),
]:
age_val = attrgenfunct.generate_normal_age(mu, sigma, min_val, max_val)
passed = True
if float(age_val) < min_val:
passed = False
if float(age_val) > max_val:
passed = False
if '.' in age_val:
passed = False
if passed == True:
num_passed += 1
else:
num_failed += 1
assert num_passed + num_failed == num_tests * 4
test_result_str = 'attrgenfunct,n/a,generate_normal_age,' + 'n/a,funct,%d,' % (
num_tests * 4
)
if num_failed == 0:
test_result_str += 'all tests passed'
else:
test_result_str += '%d tests failed' % (num_failed)
return [test_result_str, '']
# =============================================================================
# Generate a time string to be used for the log file
#
curr_time_tuple = time.localtime()
curr_time_str = (
str(curr_time_tuple[0])
+ str(curr_time_tuple[1]).zfill(2)
+ str(curr_time_tuple[2]).zfill(2)
+ '-'
+ str(curr_time_tuple[3]).zfill(2)
+ str(curr_time_tuple[4]).zfill(2)
)
# Write test output header line into the log file
#
out_file_name = './logs/attrgenfunctTest-%s.csv' % (curr_time_str)
out_file = open(out_file_name, 'w')
out_file.write('Test results generated by attrgenfunctTest.py' + os.linesep)
out_file.write('Test started: ' + curr_time_str + os.linesep)
out_file.write(os.linesep)
out_file.write(
'Module name,Class name,Method name,Arguments,Test_type,'
+ 'Patterns tested,Summary,Failure description'
+ os.linesep
)
out_file.write(os.linesep)
# Create instances for the testcase class that calls all tests
#
test_res_list = []
test_case_ins = TestCase('testArguments')
test_res_list += test_case_ins.testArguments(test_argument_data_dict)
test_case_ins = TestCase('testFunct_generate_phone_number_australia')
test_res_list += test_case_ins.testFunct_generate_phone_number_australia(num_tests)
test_case_ins = TestCase('testFunct_generate_credit_card_number')
test_res_list += test_case_ins.testFunct_generate_credit_card_number(num_tests)
test_case_ins = TestCase('testFunct_generate_uniform_value')
test_res_list += test_case_ins.testFunct_generate_uniform_value(num_tests)
test_case_ins = TestCase('testFunct_generate_uniform_age')
test_res_list += test_case_ins.testFunct_generate_uniform_age(num_tests)
test_case_ins = TestCase('testFunct_generate_normal_value')
test_res_list += test_case_ins.testFunct_generate_normal_value(num_tests)
test_case_ins = TestCase('testFunct_generate_normal_age')
test_res_list += test_case_ins.testFunct_generate_normal_age(num_tests)
# Write test output results into the log file
#
for line in test_res_list:
out_file.write(line + os.linesep)
out_file.close()
print('Test results are written to', out_file_name)
for line in test_res_list:
print(line)
|