Best Python code snippet using autotest_python
tap-gtester
Source:tap-gtester  
1#!/usr/bin/python2# Copyright (C) 2014 Red Hat, Inc.3#4# Cockpit is free software; you can redistribute it and/or modify it5# under the terms of the GNU Lesser General Public License as published by6# the Free Software Foundation; either version 2.1 of the License, or7# (at your option) any later version.8#9# Cockpit is distributed in the hope that it will be useful, but10# WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12# Lesser General Public License for more details.13#14# You should have received a copy of the GNU Lesser General Public License15# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.16#17# This is a test output compiler which produces TAP from GTest output18# if GTest output is detected.19#20# Versions of glib later than 2.38.x output TAP natively when tests are21# run with the --tap option. However we can't depend on such a recent22# version of glib for our purposes.23#24# This implements the Test Anything Protocol (ie: TAP)25# https://metacpan.org/pod/release/PETDANCE/Test-Harness-2.64/lib/Test/Harness/TAP.pod26#27import argparse28import os29import select30import signal31import subprocess32import sys33# Yes, it's dumb, but strsignal is not exposed in python34# In addition signal numbers varify heavily from arch to arch35def strsignal(sig):36    for name in dir(signal):37        if name.startswith("SIG") and sig == getattr(signal, name):38            return name39    return str(sig)40class NullCompiler:41    def __init__(self, command):42        self.command = command43    def input(self, line):44        sys.stdout.write(line)45    def process(self, proc):46        while True:47            line = proc.stdout.readline()48            if not line:49                break50            self.input(line)51        proc.wait()52        return proc.returncode53    def run(self, proc, line=None):54        if line:55            self.input(line)56        return self.process(proc)57class GTestCompiler(NullCompiler):58    def __init__(self, filename):59        NullCompiler.__init__(self, filename)60        self.test_num = 061        self.test_name = None62        self.test_remaining = []63    def input(self, line):64        line = line.strip()65        if line.startswith("GTest: "):66           (cmd, unused, data) = line[7:].partition(": ")67           cmd = cmd.strip()68           data = data.strip()69           if cmd == "run":70               self.test_name = data71               assert self.test_name in self.test_remaining, "%s %s" % (self.test_name, repr(self.test_remaining))72               self.test_remaining.remove(self.test_name)73               self.test_num += 174           elif cmd == "result":75               if self.test_name:76                   if data == "OK":77                       print "ok %d %s" % (self.test_num, self.test_name)78                   if data == "FAIL":79                       print "not ok %d %s", (self.test_num, self.test_name)80               self.test_name = None81           elif cmd == "skipping":82               if "/subprocess" not in data:83                   print "ok %d # skip -- %s" % (self.test_num, data)84               self.test_name = None85           elif data:86               print "# %s: %s" % (cmd, data)87           else:88               print "# %s" % cmd89        elif line.startswith("(MSG: "):90            print "# %s" % line[6:-1]91        elif line:92            print "# %s" % line93        sys.stdout.flush()94    def run(self, proc, output=""):95        # Complete retrieval of the list of tests96        output += proc.stdout.read()97        proc.wait()98        if proc.returncode:99            sys.stderr.write("tap-gtester: listing GTest tests failed: %d\n" % proc.returncode)100            return proc.returncode101        self.test_remaining = []102        for line in output.split("\n"):103            if line.startswith("/"):104                self.test_remaining.append(line.strip())105        if not self.test_remaining:106            print "Bail out! No tests found in GTest: %s" % self.command[0]107            return 0108        print "1..%d" % len(self.test_remaining)109        # First try to run all the tests in a batch110        proc = subprocess.Popen(self.command + ["--verbose" ], close_fds=True, stdout=subprocess.PIPE)111        result = self.process(proc)112        if result == 0:113            return 0114        if result < 0:115            print >> sys.stderr, "%s terminated with %s" % (self.command[0], strsignal(-result))116        # Now pick up any stragglers due to failures117        while True:118            # Assume that the last test failed119            if self.test_name:120                print "not ok %d %s" % (self.test_num, self.test_name)121                self.test_name = None122            # Run any tests which didn't get run123            if not self.test_remaining:124                break125            proc = subprocess.Popen(self.command + ["--verbose", "-p", self.test_remaining[0]],126                                    close_fds=True, stdout=subprocess.PIPE)127            result = self.process(proc)128            # The various exit codes and signals we continue for129            if result not in [ 0, 1, -4, -5, -6, -7, -8, -11, 33 ]:130                break131        return result132def main(argv):133    parser = argparse.ArgumentParser(description='Automake TAP compiler',134                                     usage="tap-gtester [--format FORMAT] command ...")135    parser.add_argument('--format', metavar='FORMAT', choices=[ "auto", "gtest", "tap" ],136                        default="auto", help='The input format to compile')137    parser.add_argument('--verbose', action='store_true',138                        default=True, help='Verbose mode (ignored)')139    parser.add_argument('command', nargs=argparse.REMAINDER, help="A test command to run")140    args = parser.parse_args(argv[1:])141    output = None142    format = args.format143    cmd = args.command144    if not cmd:145        print >> sys.stderr, "tap-gtester: specify a command to run"146        return 2147    if cmd[0] == '--':148        cmd.pop(0)149    proc = None150    os.environ['HARNESS_ACTIVE'] = '1'151    if format in ["auto", "gtest"]:152        list_cmd = cmd + ["-l", "--verbose"]153        proc = subprocess.Popen(list_cmd, close_fds=True, stdout=subprocess.PIPE)154        output = proc.stdout.readline()155        # Smell whether we're dealing with GTest list output from first line156        if "random seed" in output or "GTest" in output or output.startswith("/"):157            format = "gtest"158        else:159            format = "tap"160    else:161        proc = subprocess.Popen(cmd, close_fds=True, stdout=subprocess.PIPE)162    if format == "gtest":163        compiler = GTestCompiler(cmd)164    elif format == "tap":165        compiler = NullCompiler(cmd)166    else:167        assert False, "not reached"168    return compiler.run(proc, output)169if __name__ == "__main__":...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!!
