Best Python code snippet using molecule_python
testreporter.py
Source:testreporter.py  
1#!/usr/bin/env python2"""3Simple script to populate CESM test database with test results.4"""5from standard_script_setup import *6from CIME.XML.env_build             import EnvBuild7from CIME.XML.env_case              import EnvCase8from CIME.XML.env_test              import EnvTest9from CIME.XML.test_reporter         import TestReporter10from CIME.utils                     import expect11from CIME.XML.generic_xml import GenericXML12import glob13###############################################################################14def parse_command_line(args):15###############################################################################16    parser = argparse.ArgumentParser()17    CIME.utils.setup_standard_logging_options(parser)18    # Parse command line options19#    parser = argparse.ArgumentParser(description='Arguements for testreporter')20    parser.add_argument("--tagname",21                        help="Name of the tag being tested.")22    parser.add_argument("--testid",23                        help="Test id, ie c2_0_a6g_ing,c2_0_b6g_gnu.")24    parser.add_argument("--testroot",25                        help="Root directory for tests to populate the database.")26    parser.add_argument("--testtype",27                        help="Type of test, prealpha or prebeta.")28    parser.add_argument("--dryrun",action="store_true",29                        help="Do a dry run, database will not be populated.")30    parser.add_argument("--dumpxml",action="store_true",31                        help="Dump XML test results to sceen.")32    args = parser.parse_args()33    CIME.utils.parse_args_and_handle_standard_logging_options(args)34    return args.testroot, args.testid, args.tagname, args.testtype, args.dryrun, args.dumpxml35###############################################################################36def get_testreporter_xml(testroot, testid, tagname, testtype):37###############################################################################38    os.chdir(testroot)39    #40    # Retrieve compiler name and mpi library41    #42    xml_file=glob.glob("*"+testid+"/env_build.xml")43    expect(len(xml_file) > 0, "Tests not found.  It's possible your testid, {} is wrong.".format(testid))44    envxml=(EnvBuild(".",infile=xml_file[0]))45    compiler=envxml.get_value("COMPILER")46    mpilib=envxml.get_value("MPILIB")47    #48    # Retrieve machine name49    #50    xml_file=glob.glob("*"+testid+"/env_case.xml")51    envxml=(EnvCase(".",infile=xml_file[0]))52    machine=envxml.get_value("MACH")53    #54    # Retrieve baseline tag to compare to55    #56    xml_file=glob.glob("*"+testid+"/env_test.xml")57    envxml=(EnvTest(".",infile=xml_file[0]))58    baseline = envxml.get_value("BASELINE_NAME_CMP")59    #60    # Create XML header61    #62    testxml=TestReporter()63    testxml.setup_header(tagname,machine,compiler,mpilib,testroot,testtype,baseline)64    #65    # Create lists on tests based on the testid in the testroot directory.66    #67    test_names=glob.glob("*"+testid)68    #69    # Loop over all tests and parse the test results70    #71    test_status={}72    for test_name in test_names:73        if not os.path.isfile(test_name+"/TestStatus"):74            continue75        test_status['COMMENT']=""76        test_status['BASELINE']='----'77        test_status['MEMCOMP']='----'78        test_status['MEMLEAK']='----'79        test_status['NLCOMP']='----'80        test_status['STATUS']='----'81        test_status['TPUTCOMP']='----'82        #83        # Check to see if TestStatus is present, if not then continue84        # I might want to set the status to fail85        #86        try:87            lines = [line.rstrip('\n') for line in open(test_name+"/TestStatus")]88        except:89            test_status['STATUS']="FAIL"90            test_status['COMMENT']="TestStatus missing. "91            continue92        #93        # Loop over each line of TestStatus, and check for different types of failures.94        #95        for line in lines:96            if "NLCOMP" in line:97                test_status['NLCOMP']=line[0:4]98            if "MEMLEAK" in line:99                test_status['MEMLEAK']=line[0:4]100            if "MEMCOMP" in line:101                test_status['MEMCOMP']=line[0:4]102            if "BASELINE" in line:103                test_status['BASELINE']=line[0:4]104            if "TPUTCOMP" in line:105                test_status['TPUTCOMP']=line[0:4]106                if "FAIL PFS" in line:107                    test_status['STATUS']="FAIL"108            if "INIT" in line:109                test_status['INIT']=line[0:4]110                if line[0:4] in ("FAIL","PEND"):111                    test_status['STATUS']="SFAIL"112                    test_status['COMMENT']+="INIT fail! "113                    break114            if "CREATE_NEWCASE" in line:115                test_status['CREATE_NEWCASE']=line[0:4]116                if line[0:4] in ("FAIL","PEND"):117                    test_status['STATUS']="SFAIL"118                    test_status['COMMENT']+="CREATE_NEWCASE fail! "119                    break120            if "XML" in line:121                test_status['XML']=line[0:4]122                if line[0:4] in ("FAIL","PEND"):123                    test_status['STATUS']="SFAIL"124                    test_status['COMMENT']+="XML fail! "125                    break126            if "SETUP" in line:127                test_status['SETUP']=line[0:4]128                if line[0:4] in ("FAIL","PEND"):129                    test_status['STATUS']="SFAIL"130                    test_status['COMMENT']+="SETUP fail! "131                    break132            if "SHAREDLIB_BUILD" in line:133                test_status['SHAREDLIB_BUILD']=line[0:4]134                if line[0:4] in ("FAIL","PEND"):135                    test_status['STATUS']="CFAIL"136                    test_status['COMMENT']+="SHAREDLIB_BUILD fail! "137                    break138            if "MODEL_BUILD" in line:139                test_status['MODEL_BUILD']=line[0:4]140                if line[0:4] in ("FAIL","PEND"):141                    test_status['STATUS']="CFAIL"142                    test_status['COMMENT']+="MODEL_BUILD fail! "143                    break144            if "SUBMIT" in line:145                test_status['STATUS']=line[0:4]146                if line[0:4] in ("FAIL","PEND"):147                    test_status['COMMENT']+="SUBMIT fail! "148                    break149            if "RUN" in line:150                test_status['STATUS']=line[0:4]151                if line[0:4] in ("FAIL","PEND"):152                    test_status['COMMENT']+="RUN fail! "153                    break154            if "COMPARE_base_rest" in line:155                test_status['STATUS']=line[0:4]156                if line[0:4] in ("FAIL","PEND"):157                    test_status['COMMENT']+="Restart fail! "158                    break159            if "COMPARE_base_hybrid" in line:160                test_status['STATUS']=line[0:4]161                if line[0:4] in ("FAIL","PEND"):162                    test_status['COMMENT']+="Hybrid fail! "163                    break164            if "COMPARE_base_multiinst" in line:165                test_status['STATUS']=line[0:4]166                if line[0:4] in ("FAIL","PEND"):167                    test_status['COMMENT']+="Multi instance fail! "168                    break169            if "COMPARE_base_test" in line:170                test_status['STATUS']=line[0:4]171                if line[0:4] in ("FAIL","PEND"):172                    test_status['COMMENT']+="Base test fail! "173                    break174            if "COMPARE_base_single_thread" in line:175                test_status['STATUS']=line[0:4]176                if line[0:4] in ("FAIL","PEND"):177                    test_status['COMMENT']+="Thread test fail! "178                    break179            #180            #  Do not include time comments.  Just a preference to have cleaner comments in the test database181            #182            try:183                if 'time=' not in line and 'GENERATE' not in line:184                    if 'BASELINE' not in line:185                        test_status['COMMENT']+=line.split(' ',3)[3]+' '186                    else:187                        test_status['COMMENT']+=line.split(' ',4)[4]+' '188            except:189                pass190        #191        # Fill in the xml with the test results192        #193        testxml.add_result(test_name,test_status)194    return testxml195##############################################################################196def _main_func():197###############################################################################198    testroot, testid, tagname, testtype, dryrun, dumpxml = parse_command_line(sys.argv)199    testxml = get_testreporter_xml(testroot, testid, tagname, testtype)200    #201    # Dump xml to a file.202    #203    if dumpxml:204        GenericXML.write(testxml,outfile="TestRecord.xml")205    #206    # Prompt for username and password, then post the XML string to the test database website207    #208    if not dryrun:209        testxml.push2testdb()210###############################################################################211if __name__ == "__main__":...test_status.py
Source:test_status.py  
1# -*- encoding=utf-8 -*-2# Copyright 2016 David Cary; licensed under the Apache License, Version 2.03import unittest4import _test_aids5from _src import sb12886from sb1288 import status7from sb1288 import constants as K8from sb1288.constants import Decimal9from sb1288.validate import str_tuple10import re11class TestStatus(unittest.TestCase):12  """Test the status module and status.Status class"""13  def setUp(self):14    self.save_maxDiff = self.maxDiff15    self.maxDiff = None16  def tearDown(self):17    self.maxDiff = self.save_maxDiff18  def get_exception_message(self, exception_type, args):19    """Get an exception's string representation"""20    message = None21    try:22      raise exception_type(*args)23    except exception_type as exc:24      message = str(exc)25    return message26  def make_status_1(self):27    return status.Status('E', 13, 4, K.STATUS_ELECTED)28  def make_status_2(self):29    return status.Status('F', K.ONE * 17, 3, K.STATUS_CONTINUING)30  def test_status_create_1(self):31    test_status = status.Status('A')32    self.assertEqual(test_status.candidate, 'A')33    self.assertEqual(test_status.votes, 0)34    self.assertEqual(test_status.nbr_round, None)35    self.assertEqual(test_status.status, K.STATUS_CONTINUING)36  def test_status_create_2(self):37    test_status = status.Status('B', K.ZERO)38    self.assertEqual(test_status.candidate, 'B')39    self.assertEqual(test_status.votes, K.ZERO)40    self.assertEqual(test_status.nbr_round, None)41    self.assertEqual(test_status.status, K.STATUS_CONTINUING)42  def test_status_create_3(self):43    test_status = status.Status('C', K.ONE * 5, 3, K.STATUS_ELECTED)44    self.assertEqual(test_status.candidate, 'C')45    self.assertEqual(test_status.votes, K.Decimal(5))46    self.assertEqual(test_status.nbr_round, 3)47    self.assertEqual(test_status.status, K.STATUS_ELECTED)48  def test_status_create_4(self):49    status_dict = {'candidate': 'D', 'votes': 7, 'nbr_round': 2,50          'status': K.STATUS_DEFEATED}51    test_status = status.Status(status_dict)52    self.assertEqual(test_status.candidate, 'D')53    self.assertEqual(test_status.votes, 7)54    self.assertEqual(test_status.nbr_round, 2)55    self.assertEqual(test_status.status, K.STATUS_DEFEATED)56  def test_status_as_dict_1(self):57    status_dict = {'candidate': 'D', 'votes': 7, 'nbr_round': 2,58          'status': K.STATUS_DEFEATED}59    test_status = status.Status(status_dict)60    self.assertEqual(test_status.as_dict(), status_dict)61  def test_status_eq(self):62    test_status_1 = self.make_status_1()63    test_status_2 = self.make_status_1()64    test_status_3 = self.make_status_2()65    self.assertTrue(test_status_1 == test_status_2)66    self.assertFalse(test_status_1 != test_status_2)67    self.assertFalse(test_status_1 == test_status_3)68    self.assertTrue(test_status_1 != test_status_3)69  def test_status_as_str(self):70    test_status = self.make_status_1()71    test_status_as_str = str(test_status)72    self.assertEqual(str(test_status),73          "{candidate: 'E', status: 'elected', nbr_round: 4, votes: 13}")...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!!
