Best Python code snippet using autotest_python
testoutput.py
Source:testoutput.py  
1#!/usr/bin/env python2# Copyright (C) 2010 Google Inc. All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions6# are met:7#8# 1.  Redistributions of source code must retain the above copyright9#     notice, this list of conditions and the following disclaimer.10# 2.  Redistributions in binary form must reproduce the above copyright11#     notice, this list of conditions and the following disclaimer in the12#     documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24import os25import re26class NaiveImageDiffer(object):27    def same_image(self, img1, img2):28        return img1 == img229class TestOutput(object):30    """Represents the output that a single layout test generates when it is run31    on a particular platform.32    Note that this is the raw output that is produced when the layout test is33    run, not the results of the subsequent comparison between that output and34    the expected output."""35    def __init__(self, platform, output_type, files):36        self._output_type = output_type37        self._files = files38        file = files[0]  # Pick some file to do test name calculation.39        self._name = self._extract_test_name(file.name())40        self._is_actual = '-actual.' in file.name()41        self._platform = platform or self._extract_platform(file.name())42    def _extract_platform(self, filename):43        """Calculates the platform from the name of the file if it isn't known already"""44        path = filename.split(os.path.sep)45        if 'platform' in path:46            return path[path.index('platform') + 1]47        return None48    def _extract_test_name(self, filename):49        path = filename.split(os.path.sep)50        if 'LayoutTests' in path:51            path = path[1 + path.index('LayoutTests'):]52        if 'layout-test-results' in path:53            path = path[1 + path.index('layout-test-results'):]54        if 'platform' in path:55            path = path[2 + path.index('platform'):]56        filename = path[-1]57        filename = re.sub('-expected\..*$', '', filename)58        filename = re.sub('-actual\..*$', '', filename)59        path[-1] = filename60        return os.path.sep.join(path)61    def save_to(self, path):62        """Have the files in this TestOutput write themselves to the disk at the specified location."""63        for file in self._files:64            file.save_to(path)65    def is_actual(self):66        """Is this output the actual output of a test? (As opposed to expected output.)"""67        return self._is_actual68    def name(self):69        """The name of this test (doesn't include extension)"""70        return self._name71    def __eq__(self, other):72        return (other != None and73                self.name() == other.name() and74                self.type() == other.type() and75                self.platform() == other.platform() and76                self.is_actual() == other.is_actual() and77                self.same_content(other))78    def __hash__(self):79        return hash(str(self.name()) + str(self.type()) + str(self.platform()))80    def is_new_baseline_for(self, other):81        return (self.name() == other.name() and82                self.type() == other.type() and83                self.platform() == other.platform() and84                self.is_actual() and85                (not other.is_actual()))86    def __str__(self):87        actual_str = '[A] ' if self.is_actual() else ''88        return "TestOutput[%s/%s] %s%s" % (self._platform, self._output_type, actual_str, self.name())89    def type(self):90        return self._output_type91    def platform(self):92        return self._platform93    def _path_to_platform(self):94        """Returns the path that tests for this platform are stored in."""95        if self._platform is None:96            return ""97        else:98            return os.path.join("self._platform", self._platform)99    def _save_expected_result(self, file, path):100        path = os.path.join(path, self._path_to_platform())101        extension = os.path.splitext(file.name())[1]102        filename = self.name() + '-expected' + extension103        file.save_to(path, filename)104    def save_expected_results(self, path_to_layout_tests):105        """Save the files of this TestOutput to the appropriate directory106        inside the LayoutTests directory. Typically this means that these files107        will be saved in "LayoutTests/platform/<platform>/, or simply108        LayoutTests if the platform is None."""109        for file in self._files:110            self._save_expected_result(file, path_to_layout_tests)111    def delete(self):112        """Deletes the files that comprise this TestOutput from disk. This113        fails if the files are virtual files (eg: the files may reside inside a114        remote zip file)."""115        for file in self._files:116            file.delete()117class TextTestOutput(TestOutput):118    """Represents a text output of a single test on a single platform"""119    def __init__(self, platform, text_file):120        self._text_file = text_file121        TestOutput.__init__(self, platform, 'text', [text_file])122    def same_content(self, other):123        return self._text_file.contents() == other._text_file.contents()124    def retarget(self, platform):125        return TextTestOutput(platform, self._text_file)126class ImageTestOutput(TestOutput):127    image_differ = NaiveImageDiffer()128    """Represents an image output of a single test on a single platform"""129    def __init__(self, platform, image_file, checksum_file):130        self._checksum_file = checksum_file131        self._image_file = image_file132        files = filter(bool, [self._checksum_file, self._image_file])133        TestOutput.__init__(self, platform, 'image', files)134    def has_checksum(self):135        return self._checksum_file is not None136    def same_content(self, other):137        # FIXME This should not assume that checksums are up to date.138        if self.has_checksum() and other.has_checksum():139            return self._checksum_file.contents() == other._checksum_file.contents()140        else:141            self_contents = self._image_file.contents()142            other_contents = other._image_file.contents()143            return ImageTestOutput.image_differ.same_image(self_contents, other_contents)144    def retarget(self, platform):145        return ImageTestOutput(platform, self._image_file, self._checksum_file)146    def checksum(self):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
