| 1 |
""" |
| 2 |
Test the base class for interactive test cases. |
| 3 |
""" |
| 4 |
|
| 5 |
import glob |
| 6 |
from tests import mock |
| 7 |
import os |
| 8 |
import pytest |
| 9 |
import shutil |
| 10 |
from ..base.interactive import InteractiveTestCase |
| 11 |
import tempfile |
| 12 |
import unittest |
| 13 |
|
| 14 |
import pyglet |
| 15 |
from pyglet import window |
| 16 |
from pyglet.gl import * |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.requires_user_action |
| 20 |
class InteractiveTestCaseTest(InteractiveTestCase): |
| 21 |
""" |
| 22 |
Test the interactive test case base. Is an interactive test case itself, to be able to test it |
| 23 |
properly. |
| 24 |
""" |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
self._patchers = [] |
| 28 |
self._temporary_directories = [] |
| 29 |
|
| 30 |
def tearDown(self): |
| 31 |
for patcher in self._patchers: |
| 32 |
patcher.stop() |
| 33 |
for directory in self._temporary_directories: |
| 34 |
shutil.rmtree(directory) |
| 35 |
|
| 36 |
def _patch_directory(self, target): |
| 37 |
directory = tempfile.mkdtemp() |
| 38 |
self._temporary_directories.append(directory) |
| 39 |
patcher = mock.patch(target, directory) |
| 40 |
self._patchers.append(patcher) |
| 41 |
patcher.start() |
| 42 |
return directory |
| 43 |
|
| 44 |
def _patch_screenshot_paths(self): |
| 45 |
self._session_screenshot_path = self._patch_directory('tests.base.interactive.session_screenshot_path') |
| 46 |
self._committed_screenshot_path = self._patch_directory('tests.base.interactive.committed_screenshot_path') |
| 47 |
|
| 48 |
def test_single_method(self): |
| 49 |
class _Test(InteractiveTestCase): |
| 50 |
test1_ran = False |
| 51 |
|
| 52 |
def test_1(self): |
| 53 |
_Test.test1_ran = True |
| 54 |
|
| 55 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 56 |
self.assertIsNotNone(tests) |
| 57 |
self.assertEqual(tests.countTestCases(), 1) |
| 58 |
|
| 59 |
result = unittest.TestResult() |
| 60 |
tests.run(result) |
| 61 |
|
| 62 |
self.assertTrue(_Test.test1_ran, 'Test should have run') |
| 63 |
|
| 64 |
def test_multiple_methods(self): |
| 65 |
class _Test(InteractiveTestCase): |
| 66 |
test1_ran = False |
| 67 |
test2_ran = False |
| 68 |
|
| 69 |
def test_1(self): |
| 70 |
_Test.test1_ran = True |
| 71 |
|
| 72 |
def test_2(self): |
| 73 |
_Test.test2_ran = True |
| 74 |
|
| 75 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 76 |
self.assertIsNotNone(tests) |
| 77 |
self.assertEqual(tests.countTestCases(), 2) |
| 78 |
|
| 79 |
result = unittest.TestResult() |
| 80 |
tests.run(result) |
| 81 |
|
| 82 |
self.assertTrue(_Test.test1_ran, 'Test 1 should have run') |
| 83 |
self.assertTrue(_Test.test2_ran, 'Test 2 should have run') |
| 84 |
|
| 85 |
def test_user_verify_passed(self): |
| 86 |
class _Test(InteractiveTestCase): |
| 87 |
test1_ran = False |
| 88 |
|
| 89 |
def test_1(self): |
| 90 |
_Test.test1_ran = True |
| 91 |
|
| 92 |
self.user_verify('Just press Enter', take_screenshot=False) |
| 93 |
|
| 94 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 95 |
self.assertIsNotNone(tests) |
| 96 |
self.assertEqual(tests.countTestCases(), 1) |
| 97 |
|
| 98 |
result = unittest.TestResult() |
| 99 |
tests.run(result) |
| 100 |
|
| 101 |
self.assertTrue(_Test.test1_ran, 'Test should have run') |
| 102 |
self.assertEqual(len(result.failures), 0, 'Not expecting failures') |
| 103 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 104 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 105 |
|
| 106 |
self.user_verify('Did I ask you to press Enter?', take_screenshot=False) |
| 107 |
|
| 108 |
def test_user_verify_failed(self): |
| 109 |
class _Test(InteractiveTestCase): |
| 110 |
test1_ran = False |
| 111 |
|
| 112 |
def test_1(self): |
| 113 |
_Test.test1_ran = True |
| 114 |
|
| 115 |
self.user_verify('Enter "n" and then enter reason "abcd"', take_screenshot=False) |
| 116 |
|
| 117 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 118 |
self.assertIsNotNone(tests) |
| 119 |
self.assertEqual(tests.countTestCases(), 1) |
| 120 |
|
| 121 |
result = unittest.TestResult() |
| 122 |
tests.run(result) |
| 123 |
|
| 124 |
self.assertTrue(_Test.test1_ran, 'Test should have run') |
| 125 |
self.assertEqual(len(result.failures), 1, 'Expected 1 test failure') |
| 126 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 127 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 128 |
|
| 129 |
self.assertIn('AssertionError: abcd', result.failures[0][1], 'Did not get failure message entered by user.') |
| 130 |
|
| 131 |
def test_verify_commits_screenshot_on_user_passed(self): |
| 132 |
class _Test(InteractiveTestCase): |
| 133 |
def test_1(self): |
| 134 |
w = window.Window(200, 200) |
| 135 |
w.switch_to() |
| 136 |
glClearColor(1, 0, 1, 1) |
| 137 |
glClear(GL_COLOR_BUFFER_BIT) |
| 138 |
w.flip() |
| 139 |
|
| 140 |
self.user_verify('Please choose yes (or press Enter)') |
| 141 |
w.close() |
| 142 |
|
| 143 |
self._patch_screenshot_paths() |
| 144 |
|
| 145 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 146 |
self.assertIsNotNone(tests) |
| 147 |
self.assertEqual(tests.countTestCases(), 1) |
| 148 |
|
| 149 |
result = unittest.TestResult() |
| 150 |
tests.run(result) |
| 151 |
|
| 152 |
self.assertEqual(len(result.failures), 0, 'Not expecting failures') |
| 153 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 154 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 155 |
|
| 156 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 157 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 158 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 159 |
|
| 160 |
files = glob.glob(os.path.join(self._committed_screenshot_path, '*.png')) |
| 161 |
self.assertEqual(len(files), 1, 'Screenshot not committed') |
| 162 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 163 |
|
| 164 |
@mock.patch('tests.interactive.interactive_test_base.interactive', False) |
| 165 |
def test_screenshot_taken_but_not_committed_on_noninteractive_failure(self): |
| 166 |
class _Test(InteractiveTestCase): |
| 167 |
def test_1(self): |
| 168 |
w = window.Window(200, 200) |
| 169 |
w.switch_to() |
| 170 |
glClearColor(1, 0, 1, 1) |
| 171 |
glClear(GL_COLOR_BUFFER_BIT) |
| 172 |
w.flip() |
| 173 |
|
| 174 |
self.user_verify('Empty window') |
| 175 |
w.close() |
| 176 |
|
| 177 |
self.fail('Test failed') |
| 178 |
|
| 179 |
self._patch_screenshot_paths() |
| 180 |
|
| 181 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 182 |
self.assertIsNotNone(tests) |
| 183 |
self.assertEqual(tests.countTestCases(), 1) |
| 184 |
|
| 185 |
result = unittest.TestResult() |
| 186 |
tests.run(result) |
| 187 |
|
| 188 |
self.assertEqual(len(result.failures), 1, 'Expecting 1 failure') |
| 189 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 190 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 191 |
|
| 192 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 193 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 194 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 195 |
|
| 196 |
files = glob.glob(os.path.join(self._committed_screenshot_path, '*.png')) |
| 197 |
self.assertEqual(len(files), 0, 'Screenshot should not have been comitted') |
| 198 |
|
| 199 |
@mock.patch('tests.interactive.interactive_test_base.interactive', False) |
| 200 |
@mock.patch('tests.interactive.interactive_test_base.allow_missing_screenshots', True) |
| 201 |
def test_screenshot_taken_but_not_committed_on_noninteractive_pass(self): |
| 202 |
class _Test(InteractiveTestCase): |
| 203 |
def test_1(self): |
| 204 |
w = window.Window(200, 200) |
| 205 |
w.switch_to() |
| 206 |
glClearColor(1, 0, 1, 1) |
| 207 |
glClear(GL_COLOR_BUFFER_BIT) |
| 208 |
w.flip() |
| 209 |
|
| 210 |
self.user_verify('Empty window') |
| 211 |
w.close() |
| 212 |
|
| 213 |
self._patch_screenshot_paths() |
| 214 |
|
| 215 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 216 |
self.assertIsNotNone(tests) |
| 217 |
self.assertEqual(tests.countTestCases(), 1) |
| 218 |
|
| 219 |
result = unittest.TestResult() |
| 220 |
tests.run(result) |
| 221 |
|
| 222 |
self.assertEqual(len(result.failures), 0, 'Not expecting failures') |
| 223 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 224 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 225 |
|
| 226 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 227 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 228 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 229 |
|
| 230 |
files = glob.glob(os.path.join(self._committed_screenshot_path, '*.png')) |
| 231 |
self.assertEqual(len(files), 0, 'Screenshot should not have been comitted') |
| 232 |
|
| 233 |
@mock.patch('tests.interactive.interactive_test_base.interactive', False) |
| 234 |
def test_fails_on_missing_screenshot_on_noninteractive_pass(self): |
| 235 |
class _Test(InteractiveTestCase): |
| 236 |
def test_1(self): |
| 237 |
w = window.Window(200, 200) |
| 238 |
w.switch_to() |
| 239 |
glClearColor(1, 0, 1, 1) |
| 240 |
glClear(GL_COLOR_BUFFER_BIT) |
| 241 |
w.flip() |
| 242 |
|
| 243 |
self.user_verify('Empty window') |
| 244 |
w.close() |
| 245 |
|
| 246 |
self._patch_screenshot_paths() |
| 247 |
|
| 248 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 249 |
self.assertIsNotNone(tests) |
| 250 |
self.assertEqual(tests.countTestCases(), 1) |
| 251 |
|
| 252 |
result = unittest.TestResult() |
| 253 |
tests.run(result) |
| 254 |
|
| 255 |
self.assertEqual(len(result.failures), 1, 'Expecting 1 failure') |
| 256 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 257 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 258 |
|
| 259 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 260 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 261 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 262 |
|
| 263 |
files = glob.glob(os.path.join(self._committed_screenshot_path, '*.png')) |
| 264 |
self.assertEqual(len(files), 0, 'Screenshot should not have been comitted') |
| 265 |
|
| 266 |
|
| 267 |
def test_screenshot_taken_but_not_committed_on_user_failure(self): |
| 268 |
class _Test(InteractiveTestCase): |
| 269 |
def test_1(self): |
| 270 |
w = window.Window(200, 200) |
| 271 |
w.switch_to() |
| 272 |
glClearColor(1, 0, 1, 1) |
| 273 |
glClear(GL_COLOR_BUFFER_BIT) |
| 274 |
w.flip() |
| 275 |
|
| 276 |
try: |
| 277 |
self.user_verify('Please select "n" and enter any reason') |
| 278 |
finally: |
| 279 |
w.close() |
| 280 |
|
| 281 |
|
| 282 |
self._patch_screenshot_paths() |
| 283 |
|
| 284 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 285 |
self.assertIsNotNone(tests) |
| 286 |
self.assertEqual(tests.countTestCases(), 1) |
| 287 |
|
| 288 |
result = unittest.TestResult() |
| 289 |
tests.run(result) |
| 290 |
|
| 291 |
self.assertEqual(len(result.failures), 1, 'Expecting 1 failure') |
| 292 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 293 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 294 |
|
| 295 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 296 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 297 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 298 |
|
| 299 |
files = glob.glob(os.path.join(self._committed_screenshot_path, '*.png')) |
| 300 |
self.assertEqual(len(files), 0, 'Screenshot should not have been committed') |
| 301 |
|
| 302 |
@mock.patch('tests.interactive.interactive_test_base.interactive', False) |
| 303 |
def test_screenshot_does_not_match(self): |
| 304 |
class _Test(InteractiveTestCase): |
| 305 |
def test_1(self): |
| 306 |
w = window.Window(200, 200) |
| 307 |
w.switch_to() |
| 308 |
glClearColor(0, 0, 1, 1) |
| 309 |
glClear(GL_COLOR_BUFFER_BIT) |
| 310 |
w.flip() |
| 311 |
|
| 312 |
self.user_verify('Empty window') |
| 313 |
w.close() |
| 314 |
|
| 315 |
self._patch_screenshot_paths() |
| 316 |
|
| 317 |
|
| 318 |
screenshot_name = 'tests.interactive.test_interactive_test_base._Test.test_1.001.png' |
| 319 |
original_screenshot = os.path.join(os.path.dirname(__file__), '..', 'data', 'images', screenshot_name) |
| 320 |
committed_screenshot = os.path.join(self._committed_screenshot_path, screenshot_name) |
| 321 |
shutil.copy(original_screenshot, committed_screenshot) |
| 322 |
|
| 323 |
|
| 324 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 325 |
self.assertIsNotNone(tests) |
| 326 |
self.assertEqual(tests.countTestCases(), 1) |
| 327 |
|
| 328 |
result = unittest.TestResult() |
| 329 |
tests.run(result) |
| 330 |
|
| 331 |
self.assertEqual(len(result.failures), 1, 'Expecting 1 failure') |
| 332 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 333 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 334 |
|
| 335 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 336 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 337 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 338 |
|
| 339 |
|
| 340 |
original_image = pyglet.image.load(original_screenshot) |
| 341 |
committed_image = pyglet.image.load(committed_screenshot) |
| 342 |
self.assert_image_equal(original_image, committed_image, msg='Committed image should not be overwritten') |
| 343 |
|
| 344 |
@mock.patch('tests.interactive.interactive_test_base.interactive', False) |
| 345 |
def test_screenshot_matches(self): |
| 346 |
class _Test(InteractiveTestCase): |
| 347 |
def test_1(self): |
| 348 |
w = window.Window(200, 200) |
| 349 |
w.switch_to() |
| 350 |
glClearColor(1, 0, 1, 1) |
| 351 |
glClear(GL_COLOR_BUFFER_BIT) |
| 352 |
w.flip() |
| 353 |
|
| 354 |
self.user_verify('Empty window') |
| 355 |
w.close() |
| 356 |
|
| 357 |
self._patch_screenshot_paths() |
| 358 |
|
| 359 |
|
| 360 |
screenshot_name = 'tests.interactive.test_interactive_test_base._Test.test_1.001.png' |
| 361 |
original_screenshot = os.path.join(os.path.dirname(__file__), '..', 'data', 'images', screenshot_name) |
| 362 |
committed_screenshot = os.path.join(self._committed_screenshot_path, screenshot_name) |
| 363 |
shutil.copy(original_screenshot, committed_screenshot) |
| 364 |
|
| 365 |
|
| 366 |
tests = unittest.defaultTestLoader.loadTestsFromTestCase(_Test) |
| 367 |
self.assertIsNotNone(tests) |
| 368 |
self.assertEqual(tests.countTestCases(), 1) |
| 369 |
|
| 370 |
result = unittest.TestResult() |
| 371 |
tests.run(result) |
| 372 |
|
| 373 |
self.assertEqual(len(result.failures), 0, 'Not expecting failures') |
| 374 |
self.assertEqual(len(result.errors), 0, 'Not expecting errors') |
| 375 |
self.assertEqual(result.testsRun, 1, 'Expected 1 test run') |
| 376 |
|
| 377 |
files = glob.glob(os.path.join(self._session_screenshot_path, '*.png')) |
| 378 |
self.assertEqual(len(files), 1, 'Screenshot not stored in session directory') |
| 379 |
self.assertIn('tests.interactive.test_interactive_test_base._Test.test_1.001.png', files[0]) |
| 380 |
|
| 381 |
|