def test2_old_results(self): ref_image_path = non_existent_path('test2_old_results.png') # create two bogus files that pretend to be previous results: actual_img_path = Path(ref_image_path.stem + '_actual.png') actual_img_path.write_text('') diff_img_path = Path(ref_image_path.stem + '_diff.png') diff_img_path.write_text('') assert actual_img_path.exists() assert diff_img_path.exists() # check widget snapshot, with delete-old = False, verify results files still there: files_before = set(Path(__file__).parent.glob('*')) widget = QLabel('test') assert check_widget_snapshot(widget, __file__, ref_image_path.stem, delete_old_results=False) ref_image_path.unlink() assert actual_img_path.exists() assert diff_img_path.exists() files_after = set(Path(__file__).parent.glob('*')) assert files_after == files_before # check it again, this time results removed: actual_img_path_str = actual_img_path.resolve() diff_img_path_str = diff_img_path.resolve() assert check_widget_snapshot(widget, __file__, ref_image_path.stem) ref_image_path.unlink() assert not actual_img_path.exists() assert not diff_img_path.exists() files_after = set(Path(__file__).parent.glob('*')) assert files_before.difference(files_after) == set( [actual_img_path_str, diff_img_path_str]) assert files_before.issuperset(files_before)
def test_log_and_ref_folder_instead_of_file(self): ref_image_path = non_existent_path( 'test_ref_folder_instead_of_file.png') folder = Path(__file__).parent files_before = set(folder.glob('*')) widget = QLabel('test') log = Mock() check_widget_snapshot(widget, str(folder), str(ref_image_path), log=log) assert ref_image_path.exists() assert log.info.call_args == call( 'Generating ref snapshot %s in %s for widget %s', ref_image_path.name, folder, widget) expected_log = 'Generating ref snapshot test_ref_folder_instead_of_file.png in +++\\tests for widget ***' check_log_format(log.info.call_args, expected_log) files_after = set(folder.glob('*')) assert files_after.difference(files_before) == set( [ref_image_path.resolve()]) assert files_after.issuperset(files_before) ref_image_path.unlink()
def test_unequal_images(self, mocker): ref_image_path = non_existent_path('test_unequal_images.png') class ImgDiffer: def get_diff(self, image, ref_image): return QImage(image) def report(self): return "report" # generate reference: widget = QLabel('test') assert check_widget_snapshot(widget, __file__, ref_image_path.stem) # pretend label has changed, but less than tolerance (get_diff() returns None): widget = QLabel('test2') widget.setObjectName('label') mock_log = Mock() files_before = set(Path(__file__).parent.glob('*')) mock_timer = mocker.patch.object(pyqt_test_sandals, 'perf_counter', side_effect=[0, 123]) assert not check_widget_snapshot(widget, __file__, ref_image_path.stem, img_differ=ImgDiffer(), log=mock_log) assert mock_log.method_calls == [ call.info('Widget %s vs ref %s in %s (%.2f sec):', 'label', ref_image_path.name, ref_image_path.parent.resolve(), 123), call.info(' report'), call.warn( ' Snapshot has changed beyond tolerances, saving actual and diff images to folder %s:', ref_image_path.parent.resolve()), call.warn(' Saving actual image to %s', 'test_unequal_images_actual.png'), call.warn(' Saving diff image (White - |ref - widget|) to %s', 'test_unequal_images_diff.png') ] check_log_format( mock_log.info.call_args_list[0], 'Widget label vs ref test_unequal_images.png in ***\\tests (123.00 sec):' ) # confirm that no results files were create: files_after = set(Path(__file__).parent.glob('*')) ref_image_path.unlink() assert files_after.issuperset(files_before) actual_img_path = Path('test_unequal_images_actual.png') diff_img_path = Path('test_unequal_images_diff.png') assert files_after.difference(files_before) == set( [actual_img_path.resolve(), diff_img_path.resolve()]) actual_img_path.unlink() diff_img_path.unlink()
def test1_gen_first_image(self): ref_image_path = non_existent_path('test1_gen_first_image.png') files_before = set(Path(__file__).parent.glob('*')) widget = QLabel('test') check_widget_snapshot(widget, __file__, str(ref_image_path)) assert ref_image_path.exists() files_after = set(Path(__file__).parent.glob('*')) assert files_after.difference(files_before) == set( [ref_image_path.resolve()]) ref_image_path.unlink()
def test_equal_images(self): ref_image_path = non_existent_path('test_equal_images.png') # generate reference: files_before = set(Path(__file__).parent.glob('*')) widget = QLabel('test') assert check_widget_snapshot(widget, __file__, ref_image_path.stem) # re-check: should find images are identical: assert check_widget_snapshot(widget, __file__, ref_image_path.stem) assert check_widget_snapshot(widget, __file__, ref_image_path.stem) ref_image_path.unlink() files_after = set(Path(__file__).parent.glob('*')) assert files_before == files_after
def check_ok_before_elapse(): mock_sleep = mocker.patch.object(pyqt_test_sandals, 'sleep') assert check_widget_snapshot(widget_actual, __file__, ref_image_path.stem, try_sec=3) assert mock_sleep.call_count > 0
def test_unequal_images_diff_less_than_tol(self, mocker): ref_image_path = non_existent_path( 'test_unequal_images_diff_less_than_tol.png') class ImgDiffer_SameWithinTol: def get_diff(self, image, ref_image): return None def report(self): return "report" # generate reference: files_before = set(Path(__file__).parent.glob('*')) widget = QLabel('test') assert check_widget_snapshot(widget, __file__, ref_image_path.stem) # pretend label has changed, but less than tolerance (get_diff() returns None): widget = QLabel('test2') widget.setObjectName('label') mock_log = Mock() mock_timer = mocker.patch.object(pyqt_test_sandals, 'perf_counter', side_effect=[0, 123]) assert check_widget_snapshot(widget, __file__, ref_image_path.stem, img_differ=ImgDiffer_SameWithinTol(), log=mock_log) ref_image_path.unlink() assert mock_log.info.mock_calls == [ call('Widget %s vs ref %s in %s (%.2f sec):', 'label', ref_image_path.name, ref_image_path.parent.resolve(), 123), call(' report') ] expect = 'Widget label vs ref test_unequal_images_diff_less_than_tol.png in +++\\tests (123.00 sec):' check_log_format(mock_log.info.call_args_list[0], expect) # confirm that no results files were created: files_after = set(Path(__file__).parent.glob('*')) assert files_after == files_before
def test_custom_differ(self): ref_image_path = non_existent_path('test_custom_differ.png') # generate reference: widget = QLabel('test') assert check_widget_snapshot(widget, __file__, ref_image_path.stem) # pretend label has changed, but less than tolerance (get_diff() returns None): widget = QLabel('test2') widget.setObjectName('label') # first check that kwargs when custom differ given causes exception: img_differ = Mock() img_differ.get_diff.return_value = None pytest.raises(ValueError, check_widget_snapshot, widget, __file__, ref_image_path.stem, img_differ=img_differ, kwarg1=1, kwarg2=2) assert check_widget_snapshot(widget, __file__, ref_image_path.stem, img_differ=img_differ) assert len(img_differ.method_calls) == 1 assert img_differ.get_diff.call_count == 1 img_differ_class = Mock() img_differ_class.return_value.get_diff.return_value = None with patch.object(pyqt_test_sandals, 'ImgDiffer', img_differ_class) as mock_default_differ: assert check_widget_snapshot(widget, __file__, ref_image_path.stem) assert mock_default_differ.call_args_list == [call()] ref_image_path.unlink()
def test_slow_widget_elapses(self, qt_app): ref_image_path = non_existent_path('test_slow_widget_elapses.png') widget_ref = QLabel('test') widget_actual = QLabel('test2') assert check_widget_snapshot(widget_ref, __file__, ref_image_path.stem) def check_fails_on_elapse(): assert not check_widget_snapshot( widget_actual, __file__, ref_image_path.stem, try_sec=1) def change_actual(): widget_actual.setText('test') QTimer.singleShot(0, check_fails_on_elapse) QTimer.singleShot(1000, change_actual) QTimer.singleShot(1100, qt_app.quit) qt_app.exec() cleanup_check_files(fig_name=ref_image_path.stem) ref_image_path.unlink()
def create_ref(): assert check_widget_snapshot(widget_ref, __file__, ref_image_path.stem)
def check_fails_on_elapse(): assert not check_widget_snapshot( widget_actual, __file__, ref_image_path.stem, try_sec=1)