def test_same_size_all_pixel_diff(self): ref_img, actual_img, expect_diff_img = self.create_same_size_images( 101, 102, QColor(103, 104, 105, 106)) for i in range(ref_img.width()): for j in range(ref_img.height()): pixel_color = ref_img.pixelColor(i, j) actual_img.setPixelColor( i, j, QColor(*[c + 2 for c in pixel_color.getRgb()])) expect_diff_img.setPixelColor(i, j, QColor(2, 2, 2, 2)) differ = ImgDiffer() diff_img = differ.get_diff(actual_img, ref_img) assert expect_diff_img == diff_img assert differ.num_diffs_perc == 100 assert differ.max_pix_diff == 2 assert isclose(differ.diff_rms_perc, 100 * 2 / 255) report = differ.report() assert report == "RMS diff=0.78% (rms_tol_perc=0.00%), number of pixels changed=100.00% (num_tol_perc=None), max pix diff=2 (max_pix_diff_tol=None)" # various cases that should produce no diff: assert ImgDiffer(rms_tol_perc=1).get_diff(actual_img, ref_img) is None assert ImgDiffer(max_pix_diff_tol=2).get_diff(actual_img, ref_img) is None assert ImgDiffer(rms_tol_perc=1, max_pix_diff_tol=2).get_diff( actual_img, ref_img) is None # various cases that should produce same diff: assert ImgDiffer(rms_tol_perc=1 / 3).get_diff( actual_img, ref_img) == expect_diff_img assert ImgDiffer(max_pix_diff_tol=1).get_diff( actual_img, ref_img) == expect_diff_img
def test_same_img(self): widget1 = QLabel('test1') widget2 = QLabel('test1') ref_img = widget1.grab().toImage() img = widget2.grab().toImage() assert img == ref_img differ = ImgDiffer() assert differ.get_diff(img, ref_img) is None expect = 'RMS diff=0.00% (rms_tol_perc=0.00%), number of pixels changed=0.00% (num_tol_perc=None), max pix diff=0 (max_pix_diff_tol=None)' assert differ.report() == expect
def test(): widget_ref.show() widget_actual.show() ref_img = widget_ref.grab().toImage() img = widget_actual.grab().toImage() assert img != ref_img assert img.width() < ref_img.width() assert img.height() > ref_img.height() differ = ImgDiffer() diff = differ.get_diff(img, ref_img) # diff.save('actual_higher_thinner_diff.png') expect = QPixmap('actual_higher_thinner_diff.png') assert expect.toImage() == diff self.app.closeAllWindows()
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 test(): widget_actual.show() widget_ref.setFixedSize(widget_actual.width(), widget_actual.height()) widget_ref.show() ref_img = widget_ref.grab().toImage() img = widget_actual.grab().toImage() assert img != ref_img assert img.size() == ref_img.size() differ = ImgDiffer() diff = differ.get_diff(img, ref_img) # diff.save('same_size_img_neq.png') expect = QPixmap('same_size_img_neq.png') assert expect.toImage() == diff report = differ.report() assert report == "RMS diff=37.22% (rms_tol_perc=0.00%), number of pixels changed=10.46% (num_tol_perc=None), max pix diff=240 (max_pix_diff_tol=None)" self.app.closeAllWindows()
def test_same_size_one_pixel_diff(self): ref_img, actual_img, expect_diff_img = self.create_same_size_images( 101, 102, QColor(103, 104, 105, 106)) actual_img.setPixelColor(50, 50, QColor(0, 0, 0, 0)) expect_diff_img.setPixelColor(50, 50, QColor(103, 104, 105, 106)) differ = ImgDiffer() diff_img = differ.get_diff(actual_img, ref_img) assert expect_diff_img == diff_img assert differ.num_diffs_perc == 1 / (101 * 102) * 100 assert differ.max_pix_diff == 106 assert differ.diff_rms_perc == 100 * sqrt( pow(103, 2) + pow(104, 2) + pow(105, 2) + pow(106, 2)) / 2 / 255 report = differ.report() assert report == "RMS diff=40.98% (rms_tol_perc=0.00%), number of pixels changed=0.01% (num_tol_perc=None), max pix diff=106 (max_pix_diff_tol=None)" # various cases that should produce no diff: assert ImgDiffer(rms_tol_perc=42).get_diff(actual_img, ref_img) is None assert ImgDiffer(num_tol_perc=0.01).get_diff(actual_img, ref_img) is None assert ImgDiffer(max_pix_diff_tol=110).get_diff(actual_img, ref_img) is None assert ImgDiffer( rms_tol_perc=42, num_tol_perc=0.01, max_pix_diff_tol=110).get_diff( actual_img, ref_img) is None # various cases that should produce same diff: assert ImgDiffer(rms_tol_perc=40).get_diff(actual_img, ref_img) == expect_diff_img assert ImgDiffer(num_tol_perc=0.001).get_diff( actual_img, ref_img) == expect_diff_img assert ImgDiffer(max_pix_diff_tol=100).get_diff( actual_img, ref_img) == expect_diff_img