-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_case.py
1108 lines (817 loc) · 43.9 KB
/
test_case.py
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Test Rotest's TestCase class behavior."""
# pylint: disable=missing-docstring,unused-argument,protected-access
# pylint: disable=no-member,no-self-use,too-many-public-methods,invalid-name
# pylint: disable=too-many-lines
from __future__ import absolute_import
import os
import re
import six
from future.builtins import next
from future.utils import iteritems
from rotest.core.case import request
from rotest.management.base_resource import ResourceAdapter
from rotest.core.models.case_data import TestOutcome, CaseData
from rotest.management.models.ut_models import DemoResourceData
from rotest.management.models.ut_resources import (DemoResource,
DemoResource2,
NonExistingResource,
DemoComplexResource,
DemoAdaptiveComplexResource)
from tests.core.utils import (ErrorInSetupCase, SuccessCase, FailureCase,
ErrorCase, StoreMultipleFailuresCase,
UnexpectedSuccessCase, BasicRotestUnitTest,
DynamicResourceLockingCase, ExpectRaisesCase,
StoreFailureErrorCase, ExpectedFailureCase,
StoreFailureCase, MockTestSuite, SkipCase)
RESOURCE_NAME = 'available_resource1'
class TempSuccessCase(SuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempSkipCase(SkipCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempComplexRequestCase(SuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('res1', DemoResource, name='available_resource1'),)
res2 = DemoResource.request(name='available_resource2')
class TempComplexAdaptiveResourceCase(SuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('res1', DemoAdaptiveComplexResource),)
class TempAdaptiveRequestPositiveCase(SuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = ()
res = ResourceAdapter(config_key='field1',
resource_classes={False: DemoResource,
True: DemoComplexResource},
name='available_resource1')
class TempAdaptiveRequestNegativeCase(SuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = ()
res = ResourceAdapter(config_key='field1',
resource_classes={True: DemoResource,
False: DemoComplexResource},
name='available_resource1')
class TempInheritRequestCase(TempComplexRequestCase):
"""Inherit one resource requests from the parent class."""
__test__ = False
resources = ()
class TempDynamicResourceLockingCase(DynamicResourceLockingCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempFailureCase(FailureCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempStoreFailureCase(StoreFailureCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempExpectRaisesCase(ExpectRaisesCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempErrorCase(ErrorCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempStoreFailureErrorCase(StoreFailureErrorCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempErrorInSetupCase(ErrorInSetupCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempExpectedFailureCase(ExpectedFailureCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempUnexpectedSuccessCase(UnexpectedSuccessCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class TempStoreMultipleFailuresCase(StoreMultipleFailuresCase):
"""Inherit class and override resources requests."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
class ForceReleaseCase(SuccessCase):
"""Inherit class and override force initialize flag."""
__test__ = False
resources = (request('test_resource', DemoResource, name=RESOURCE_NAME),)
def request_resources(self, resources_to_request, use_previous=False,
force_initialize=False):
return super(ForceReleaseCase, self).request_resources(
resources_to_request, use_previous, True)
class TestTestCase(BasicRotestUnitTest):
"""Test TestCase in different scenarios.
Attributes:
result (unittest.TestResult): used in tests as run result object.
"""
fixtures = ['resource_ut.json']
LOCK_TIMEOUT = 4
FAKE_OWNER = 'owner'
DEMO_RESOURCE_NAME = 'test_resource'
def setUp(self):
"""Initialize and connect a client to resource manager.
Create the basic fixture for the following tests.
"""
super(TestTestCase, self).setUp()
self.version = 1
self.ip_address = '1.1.1.1'
def _run_case(self, test_case, **kwargs):
"""Run given case and return it.
Args:
test_case (rotest.core.case.TestCase): case to run.
Returns:
rotest.core.case.TestCase. the case.
"""
class InternalSuite(MockTestSuite):
components = (test_case,)
test_suite = InternalSuite()
case = test_suite._tests[0]
for name, value in iteritems(kwargs):
setattr(case, name, value)
self.run_test(test_suite)
return next(iter(test_suite))
def test_success_case_run(self):
"""Test a TestCase on successful run.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates the result.
* Validates the case's data object.
* Validates the resource's state.
"""
TempSuccessCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempSuccessCase)
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
self.assertEqual(case.data.exception_type, TestOutcome.SUCCESS,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.SUCCESS, case.data.exception_type))
test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]
self.assertTrue(isinstance(test_resource, DemoResource),
"State resource data type should have been 'DemoResourceData'")
self.assertEqual(test_resource.mode,
DemoResourceData.BOOT_MODE,
"State resource mode attribute should "
"have been 'boot'")
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_complex_resource_request(self):
"""Test a TestCase with all the ways to request resources.
* Way 1 - overriding 'resources'.
* Way 2 - declaring fields with BaseResource instance.
"""
case = self._run_case(TempComplexRequestCase)
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
test_resources = case.all_resources
locked_names = []
for resource in six.itervalues(test_resources):
self.assertTrue(isinstance(resource, DemoResource),
"Got wrong resource %r for the request" % resource)
test_resource = DemoResourceData.objects.get(name=resource.name)
self.validate_resource(test_resource)
locked_names.append(resource.name)
self.assertEqual(len(test_resources), 2,
"Unexpected number of resources, expected %r got %r" %
(2, len(test_resources)))
for request_name in ['res1', 'res2']:
self.assertTrue(request_name in test_resources,
"Test didn't request a resource for %s" %
request_name)
self.assertTrue(request_name in case.__dict__,
"Test doesn't contain field named %s" %
request_name)
self.assertIn('available_resource1', locked_names,
"Resource request using 'resources' ignored kwargs")
self.assertIn('available_resource2', locked_names,
"Resource request using class field ignored kwargs")
def test_complex_adaptive_resource_request(self):
"""Test a TestCase that requests a complex adaptive resources."""
case = self._run_case(TempComplexAdaptiveResourceCase,
config={'field1': False})
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
test_resources = case.all_resources
self.assertEqual(len(test_resources), 1,
"Unexpected number of resources, expected %r got %r" %
(1, len(test_resources)))
resource, = list(test_resources.values())
self.assertTrue(isinstance(resource.sub_res1, DemoResource2),
"Got wrong resource %r for sub_res1" % resource)
self.assertTrue(isinstance(resource.sub_res2, DemoResource2),
"Got wrong resource %r for sub_res2" % resource)
def test_inherit_resource_request(self):
"""Test a TestCase that inherits its resource request."""
case = self._run_case(TempInheritRequestCase)
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
test_resources = case.all_resources
self.assertEqual(len(test_resources), 1,
"Unexpected number of resources, expected %r got %r" %
(1, len(test_resources)))
request_name = 'res2'
self.assertTrue(request_name in test_resources,
"Test didn't request a resource for %s" % request_name)
self.assertTrue(request_name in case.__dict__,
"Test doesn't contain field named %s" %
request_name)
def test_adaptive_request_positive(self):
"""Test a positive TestCase with a resource adapter.
* Using the kwargs, ask for an existing resource.
"""
case = self._run_case(TempAdaptiveRequestPositiveCase,
config={'field1': False})
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
test_resources = case.all_resources
locked_names = []
for resource in six.itervalues(test_resources):
self.assertTrue(isinstance(resource, DemoResource),
"Got wrong resource %r for the request" % resource)
test_resource = DemoResourceData.objects.get(name=resource.name)
self.validate_resource(test_resource)
locked_names.append(resource.name)
self.assertEqual(len(test_resources), 1,
"Unexpected number of resources, expected %r got %r" %
(1, len(test_resources)))
request_name = 'res'
self.assertTrue(request_name in test_resources,
"Test didn't request a resource for %s" %
request_name)
self.assertTrue(request_name in case.__dict__,
"Test doesn't contain field named %s" %
request_name)
self.assertIn('available_resource1', locked_names,
"Resource request using 'resources' ignored kwargs")
def test_adaptive_request_negative(self):
"""Test a negative TestCase with a resource adapter.
* Using the kwargs, ask for a non existing resource.
"""
case = self._run_case(TempAdaptiveRequestNegativeCase,
config={'field1': False})
self.assertFalse(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertFalse(case.data.success)
test_resources = case.all_resources
self.assertEqual(len(test_resources), 0,
"Unexpected number of resources, expected %r got %r" %
(0, len(test_resources)))
def test_dynamic_resources_locking(self):
"""Test that cases can dynamically lock resources.
* Run a test that dynamically requests resources.
* Validate that the resources were initialized and finalized.
"""
TempDynamicResourceLockingCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
dynamic_resource_name = 'available_resource2'
TempDynamicResourceLockingCase.dynamic_resources = (
request('dynamic_resource',
DemoResource,
name=dynamic_resource_name),)
case = self._run_case(TempDynamicResourceLockingCase)
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success)
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
test_resource = DemoResourceData.objects.get(
name=dynamic_resource_name)
self.validate_resource(test_resource)
def test_failed_case_run(self):
"""Test a TestCase on run failure.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test failed.
* Validates the case's data object.
* Validates the resource's state.
"""
TempFailureCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempFailureCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.FAILED,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.FAILED, case.data.exception_type))
test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]
self.assertTrue(isinstance(test_resource, DemoResource),
"State resource type should have been 'DemoResource'")
self.assertEqual(test_resource.mode,
DemoResourceData.BOOT_MODE,
"State resource mode attribute should "
"have been 'boot'")
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_expect_and_assert_case_run(self):
"""Test a TestCase that uses both expect and assert.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test failed with both tracebacks.
* Validates the case's data object.
* Validates the resource's state.
"""
TempStoreFailureCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempStoreFailureCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.FAILED,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.FAILED, case.data.exception_type))
expected_traceback = "%s.*%s.*" % (StoreFailureCase.FAILURE_MESSAGE,
StoreFailureCase.ASSERTION_MESSAGE)
match = re.search(expected_traceback, case.data.traceback,
flags=re.DOTALL)
self.assertIsNotNone(match,
"Unexpected traceback, %r doesn't match the expression %r"
% (case.data.traceback, expected_traceback))
test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]
self.assertTrue(isinstance(test_resource, DemoResource),
"State resource type should have been 'DemoResource'")
self.assertEqual(test_resource.mode,
DemoResourceData.BOOT_MODE,
"State resource mode attribute should "
"have been 'boot'")
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_expect_raises_and_assert_case_run(self):
"""Test a TestCase that uses both expect raises and assert.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test failed with three tracebacks.
* Validates the case's data object.
* Validates the resource's state.
"""
TempExpectRaisesCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempExpectRaisesCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.FAILED,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.FAILED, case.data.exception_type))
expected_traceback = "%s.*%s.*%s.*" %\
(TempExpectRaisesCase.FAILURE_MESSAGE,
TempExpectRaisesCase.FAILURE_MESSAGE,
TempExpectRaisesCase.ASSERTION_MESSAGE)
match = re.search(expected_traceback, case.data.traceback,
flags=re.DOTALL)
self.assertIsNotNone(match, "Unexpected traceback, "
"%r doesn't match the expression %r"
% (case.data.traceback, expected_traceback))
test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]
self.assertTrue(isinstance(test_resource, DemoResource),
"State resource type should have been 'DemoResource'")
self.assertEqual(test_resource.mode,
DemoResourceData.BOOT_MODE,
"State resource mode attribute should "
"have been 'boot'")
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_stored_multiple_failures_case_run(self):
"""Test a TestCase that stores failures.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test failed.
* Validates the case's data object.
* Validates the resource's state.
"""
TempStoreMultipleFailuresCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempStoreMultipleFailuresCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.FAILED,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.FAILED, case.data.exception_type))
expected_traceback = "%s.*%s.*%s.*" % (
TempStoreMultipleFailuresCase.FAILURE_MESSAGE1,
CaseData.TB_SEPARATOR,
TempStoreMultipleFailuresCase.FAILURE_MESSAGE2)
match = re.search(expected_traceback, case.data.traceback,
flags=re.DOTALL)
self.assertIsNotNone(match,
"Unexpected traceback, %r doesn't match the expression %r"
% (case.data.traceback, expected_traceback))
test_resource = case.all_resources[self.DEMO_RESOURCE_NAME]
self.assertTrue(isinstance(test_resource, DemoResource),
"State resource type should have been 'DemoResource'")
self.assertEqual(test_resource.mode,
DemoResourceData.BOOT_MODE,
"State resource mode attribute should "
"have been 'boot'")
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_error_and_stored_failure_case_run(self):
"""Test a TestCase on that stores a failure and raises an exception.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resource's state.
"""
TempStoreFailureErrorCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempStoreFailureErrorCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
expected_traceback = "%s.*%s.*%s.*" % (
TempStoreFailureErrorCase.FAILURE_MESSAGE,
CaseData.TB_SEPARATOR,
TempStoreFailureErrorCase.ERROR_MESSAGE)
match = re.search(expected_traceback, case.data.traceback,
flags=re.DOTALL)
self.assertIsNotNone(match,
"Unexpected traceback, %r doesn't match the expression %r"
% (case.data.traceback, expected_traceback))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_errored_case_run(self):
"""Test a TestCase on run error.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resource's state.
"""
TempErrorCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempErrorCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_error_in_setup(self):
"""Test a TestCase on setup error.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resource's state.
"""
TempErrorInSetupCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempErrorInSetupCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource,
initialized=True, finalized=True)
def test_error_in_resource_initialize_good_first(self):
"""Test a TestCase on resource initialization error on the second.
* Defines the registered resources as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resources' state.
"""
fail_resource_name = 'fail_initialize_resource'
TempSuccessCase.resources = (request('ok_resource',
DemoResource,
name=RESOURCE_NAME),
request('fail_resource',
DemoResource,
name=fail_resource_name))
case = self._run_case(TempSuccessCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
ok_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
fail_resource = DemoResourceData.objects.get(name=fail_resource_name)
self.validate_resource(ok_resource,
initialized=True, finalized=True)
self.validate_resource(fail_resource,
initialized=False, finalized=True)
def test_error_in_resource_initialize_bad_first(self):
"""Test a TestCase on resource initialization error on the first one.
* Defines the registered resources as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resources' state.
"""
fail_resource_name = 'fail_initialize_resource'
TempSuccessCase.resources = (request('fail_resource',
DemoResource,
name=fail_resource_name),
request('ok_resource',
DemoResource,
name=RESOURCE_NAME))
case = self._run_case(TempSuccessCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
ok_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
fail_resource = DemoResourceData.objects.get(name=fail_resource_name)
self.validate_resource(fail_resource,
initialized=False, finalized=True)
self.validate_resource(ok_resource, validated=False,
initialized=False, finalized=False)
def test_error_in_resource_finalize(self):
"""Test a TestCase on resource finalization error.
* Defines the registered resources as required resource.
* Runs the test under a test suite.
* Validates that the test fails.
* Validates the case's data object.
* Validates the resource's state.
"""
fail_resource_name = 'fail_finalize_resource'
TempSuccessCase.resources = (request('fail_resource',
DemoResource,
name=fail_resource_name),
request('ok_resource',
DemoResource,
name=RESOURCE_NAME))
case = self._run_case(TempSuccessCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been False')
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
fail_resource = DemoResourceData.objects.get(name=fail_resource_name)
self.validate_resource(test_resource,
initialized=True, finalized=True)
self.validate_resource(fail_resource,
initialized=True, finalized=False)
def test_expected_failure_case_run(self):
"""Test a TestCase run on expected failure.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test fails as expected.
* Validates the case's data object.
* Validates the resource's state.
"""
TempExpectedFailureCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempExpectedFailureCase)
self.assertTrue(self.result.wasSuccessful(),
'Case failed when it should have succeeded')
# === Validate case data object ===
self.assertTrue(case.data.success,
'Case data result should have been True')
self.assertEqual(case.data.exception_type,
TestOutcome.EXPECTED_FAILURE,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.EXPECTED_FAILURE,
case.data.exception_type))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_unexpected_success_case_run(self):
"""Test a TestCase run on unexpected success.
* Defines the registered resource as required resource.
* Runs the test under a test suite.
* Validates that the test succeeds.
* Validates the case's data object.
* Validates the resource's state.
"""
TempUnexpectedSuccessCase.resources = (request('test_resource',
DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempUnexpectedSuccessCase)
self.assertFalse(self.result.wasSuccessful(),
'Case succeeded when it should have failed')
# === Validate case data object ===
self.assertFalse(case.data.success,
'Case data result should have been True')
self.assertEqual(case.data.exception_type,
TestOutcome.UNEXPECTED_SUCCESS,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.UNEXPECTED_SUCCESS,
case.data.exception_type))
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource)
def test_missing_resource(self):
"""Test a TestCase with a missing required resource.
* Defines a missing resource as required resource.
* Runs the test under a test suite
* Validates that the test failed.
"""
TempSuccessCase.resources = (request('missing_resource',
NonExistingResource,
name='missing'),)
case = self._run_case(TempSuccessCase)
# === Validate case data object ===
self.assertFalse(case.data.success)
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
def test_locked_resource(self):
"""Test a TestCase with a locked required resource.
* Locks a resource using the resource manager.
* Defines one available and one locked resource as required resources.
* Runs the test under a test suite.
* Validates the result is failure and the resources' state.
"""
available_resource_name = 'available_resource2'
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
test_resource.owner = self.FAKE_OWNER
test_resource.save()
TempSuccessCase.resources = (request('available_resource1',
DemoResource,
name=available_resource_name),
request('locked_resource',
DemoResource,
name=RESOURCE_NAME))
case = self._run_case(TempSuccessCase)
# === Validate case data object ===
self.assertFalse(case.data.success)
self.assertEqual(case.data.exception_type, TestOutcome.ERROR,
"Unexpected test outcome, expected %r got %r" %
(TestOutcome.ERROR, case.data.exception_type))
available_resource = DemoResourceData.objects.get(
name=available_resource_name)
test_resource = DemoResourceData.objects.get(name=RESOURCE_NAME)
self.validate_resource(test_resource, validated=False,
initialized=False, finalized=False)
self.validate_resource(available_resource, validated=False,
initialized=False, finalized=False)
def test_save_state_on_failed_test(self):
"""Test the usage of the save_state flag when a test has failed.
When a test has failed it is expected that it WILL save the state.
"""
resource_name = 'store_resource'
TempErrorCase.resources = (request(resource_name=resource_name,
resource_class=DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempErrorCase, save_state=True)
expected_state_path = os.path.join(case.work_dir,
TempErrorCase.STATE_DIR_NAME)
self.assertTrue(os.path.exists(expected_state_path))
def test_save_state_on_passed_test(self):
"""Test the usage of the save_state flag when a test has passed.
When a test has passed it is expected that it WILL NOT save the state.
"""
resource_name = 'save_state_resource'
TempSuccessCase.resources = (request(resource_name=resource_name,
resource_class=DemoResource,
name=RESOURCE_NAME),)
case = self._run_case(TempSuccessCase, save_state=True)
expected_state_path = os.path.join(case.work_dir,
TempSuccessCase.STATE_DIR_NAME)
self.assertFalse(os.path.exists(expected_state_path))
def test_save_state_on_expected_failure_test(self):
"""Test the usage of the save_state flag when a test has
passed with an expected failure.
When a test has passed it is expected that it WILL NOT save the state.