Best Python code snippet using unittest-xml-reporting_python
gtest_output_test.py
Source:gtest_output_test.py
1#!/usr/bin/env python2#3# Copyright 2008, Google Inc.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are8# met:9#10# * Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# * Redistributions in binary form must reproduce the above13# copyright notice, this list of conditions and the following disclaimer14# in the documentation and/or other materials provided with the15# distribution.16# * Neither the name of Google Inc. nor the names of its17# contributors may be used to endorse or promote products derived from18# this software without specific prior written permission.19#20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31"""Tests the text output of Google C++ Testing Framework.32SYNOPSIS33 gtest_output_test.py --build_dir=BUILD/DIR --gengolden34 # where BUILD/DIR contains the built gtest_output_test_ file.35 gtest_output_test.py --gengolden36 gtest_output_test.py37"""38__author__ = 'wan@google.com (Zhanyong Wan)'39import difflib40import os41import re42import sys43import gtest_test_utils44# The flag for generating the golden file45GENGOLDEN_FLAG = '--gengolden'46CATCH_EXCEPTIONS_ENV_VAR_NAME = 'GTEST_CATCH_EXCEPTIONS'47IS_WINDOWS = os.name == 'nt'48# TODO(vladl@google.com): remove the _lin suffix.49GOLDEN_NAME = 'gtest_output_test_golden_lin.txt'50PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_output_test_')51# At least one command we exercise must not have the52# 'internal_skip_environment_and_ad_hoc_tests' argument.53COMMAND_LIST_TESTS = ({}, [PROGRAM_PATH, '--gtest_list_tests'])54COMMAND_WITH_COLOR = ({}, [PROGRAM_PATH, '--gtest_color=yes'])55COMMAND_WITH_TIME = ({}, [PROGRAM_PATH,56 '--gtest_print_time',57 'internal_skip_environment_and_ad_hoc_tests',58 '--gtest_filter=FatalFailureTest.*:LoggingTest.*'])59COMMAND_WITH_DISABLED = (60 {}, [PROGRAM_PATH,61 '--gtest_also_run_disabled_tests',62 'internal_skip_environment_and_ad_hoc_tests',63 '--gtest_filter=*DISABLED_*'])64COMMAND_WITH_SHARDING = (65 {'GTEST_SHARD_INDEX': '1', 'GTEST_TOTAL_SHARDS': '2'},66 [PROGRAM_PATH,67 'internal_skip_environment_and_ad_hoc_tests',68 '--gtest_filter=PassingTest.*'])69GOLDEN_PATH = os.path.join(gtest_test_utils.GetSourceDir(), GOLDEN_NAME)70def ToUnixLineEnding(s):71 """Changes all Windows/Mac line endings in s to UNIX line endings."""72 return s.replace('\r\n', '\n').replace('\r', '\n')73def RemoveLocations(test_output):74 """Removes all file location info from a Google Test program's output.75 Args:76 test_output: the output of a Google Test program.77 Returns:78 output with all file location info (in the form of79 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or80 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by81 'FILE_NAME:#: '.82 """83 return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\: ', r'\1:#: ', test_output)84def RemoveStackTraceDetails(output):85 """Removes all stack traces from a Google Test program's output."""86 # *? means "find the shortest string that matches".87 return re.sub(r'Stack trace:(.|\n)*?\n\n',88 'Stack trace: (omitted)\n\n', output)89def RemoveStackTraces(output):90 """Removes all traces of stack traces from a Google Test program's output."""91 # *? means "find the shortest string that matches".92 return re.sub(r'Stack trace:(.|\n)*?\n\n', '', output)93def RemoveTime(output):94 """Removes all time information from a Google Test program's output."""95 return re.sub(r'\(\d+ ms', '(? ms', output)96def RemoveTypeInfoDetails(test_output):97 """Removes compiler-specific type info from Google Test program's output.98 Args:99 test_output: the output of a Google Test program.100 Returns:101 output with type information normalized to canonical form.102 """103 # some compilers output the name of type 'unsigned int' as 'unsigned'104 return re.sub(r'unsigned int', 'unsigned', test_output)105def NormalizeToCurrentPlatform(test_output):106 """Normalizes platform specific output details for easier comparison."""107 if IS_WINDOWS:108 # Removes the color information that is not present on Windows.109 test_output = re.sub('\x1b\\[(0;3\d)?m', '', test_output)110 # Changes failure message headers into the Windows format.111 test_output = re.sub(r': Failure\n', r': error: ', test_output)112 # Changes file(line_number) to file:line_number.113 test_output = re.sub(r'((\w|\.)+)\((\d+)\):', r'\1:\3:', test_output)114 return test_output115def RemoveTestCounts(output):116 """Removes test counts from a Google Test program's output."""117 output = re.sub(r'\d+ tests?, listed below',118 '? tests, listed below', output)119 output = re.sub(r'\d+ FAILED TESTS',120 '? FAILED TESTS', output)121 output = re.sub(r'\d+ tests? from \d+ test cases?',122 '? tests from ? test cases', output)123 output = re.sub(r'\d+ tests? from ([a-zA-Z_])',124 r'? tests from \1', output)125 return re.sub(r'\d+ tests?\.', '? tests.', output)126def RemoveMatchingTests(test_output, pattern):127 """Removes output of specified tests from a Google Test program's output.128 This function strips not only the beginning and the end of a test but also129 all output in between.130 Args:131 test_output: A string containing the test output.132 pattern: A regex string that matches names of test cases or133 tests to remove.134 Returns:135 Contents of test_output with tests whose names match pattern removed.136 """137 test_output = re.sub(138 r'.*\[ RUN \] .*%s(.|\n)*?\[( FAILED | OK )\] .*%s.*\n' % (139 pattern, pattern),140 '',141 test_output)142 return re.sub(r'.*%s.*\n' % pattern, '', test_output)143def NormalizeOutput(output):144 """Normalizes output (the output of gtest_output_test_.exe)."""145 output = ToUnixLineEnding(output)146 output = RemoveLocations(output)147 output = RemoveStackTraceDetails(output)148 output = RemoveTime(output)149 return output150def GetShellCommandOutput(env_cmd):151 """Runs a command in a sub-process, and returns its output in a string.152 Args:153 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra154 environment variables to set, and element 1 is a string with155 the command and any flags.156 Returns:157 A string with the command's combined standard and diagnostic output.158 """159 # Spawns cmd in a sub-process, and gets its standard I/O file objects.160 # Set and save the environment properly.161 environ = os.environ.copy()162 environ.update(env_cmd[0])163 p = gtest_test_utils.Subprocess(env_cmd[1], env=environ)164 return p.output165def GetCommandOutput(env_cmd):166 """Runs a command and returns its output with all file location167 info stripped off.168 Args:169 env_cmd: The shell command. A 2-tuple where element 0 is a dict of extra170 environment variables to set, and element 1 is a string with171 the command and any flags.172 """173 # Disables exception pop-ups on Windows.174 environ, cmdline = env_cmd175 environ = dict(environ) # Ensures we are modifying a copy.176 environ[CATCH_EXCEPTIONS_ENV_VAR_NAME] = '1'177 return NormalizeOutput(GetShellCommandOutput((environ, cmdline)))178def GetOutputOfAllCommands():179 """Returns concatenated output from several representative commands."""180 return (GetCommandOutput(COMMAND_WITH_COLOR) +181 GetCommandOutput(COMMAND_WITH_TIME) +182 GetCommandOutput(COMMAND_WITH_DISABLED) +183 GetCommandOutput(COMMAND_WITH_SHARDING))184test_list = GetShellCommandOutput(COMMAND_LIST_TESTS)185SUPPORTS_DEATH_TESTS = 'DeathTest' in test_list186SUPPORTS_TYPED_TESTS = 'TypedTest' in test_list187SUPPORTS_THREADS = 'ExpectFailureWithThreadsTest' in test_list188SUPPORTS_STACK_TRACES = False189CAN_GENERATE_GOLDEN_FILE = (SUPPORTS_DEATH_TESTS and190 SUPPORTS_TYPED_TESTS and191 SUPPORTS_THREADS and192 not IS_WINDOWS)193class GTestOutputTest(gtest_test_utils.TestCase):194 def RemoveUnsupportedTests(self, test_output):195 if not SUPPORTS_DEATH_TESTS:196 test_output = RemoveMatchingTests(test_output, 'DeathTest')197 if not SUPPORTS_TYPED_TESTS:198 test_output = RemoveMatchingTests(test_output, 'TypedTest')199 test_output = RemoveMatchingTests(test_output, 'TypedDeathTest')200 test_output = RemoveMatchingTests(test_output, 'TypeParamDeathTest')201 if not SUPPORTS_THREADS:202 test_output = RemoveMatchingTests(test_output,203 'ExpectFailureWithThreadsTest')204 test_output = RemoveMatchingTests(test_output,205 'ScopedFakeTestPartResultReporterTest')206 test_output = RemoveMatchingTests(test_output,207 'WorksConcurrently')208 if not SUPPORTS_STACK_TRACES:209 test_output = RemoveStackTraces(test_output)210 return test_output211 def testOutput(self):212 output = GetOutputOfAllCommands()213 golden_file = open(GOLDEN_PATH, 'rb')214 # A mis-configured source control system can cause \r appear in EOL215 # sequences when we read the golden file irrespective of an operating216 # system used. Therefore, we need to strip those \r's from newlines217 # unconditionally.218 golden = ToUnixLineEnding(golden_file.read())219 golden_file.close()220 # We want the test to pass regardless of certain features being221 # supported or not.222 # We still have to remove type name specifics in all cases.223 normalized_actual = RemoveTypeInfoDetails(output)224 normalized_golden = RemoveTypeInfoDetails(golden)225 if CAN_GENERATE_GOLDEN_FILE:226 self.assertEqual(normalized_golden, normalized_actual,227 '\n'.join(difflib.unified_diff(228 normalized_golden.split('\n'),229 normalized_actual.split('\n'),230 'golden', 'actual')))231 else:232 normalized_actual = NormalizeToCurrentPlatform(233 RemoveTestCounts(normalized_actual))234 normalized_golden = NormalizeToCurrentPlatform(235 RemoveTestCounts(self.RemoveUnsupportedTests(normalized_golden)))236 # This code is very handy when debugging golden file differences:237 if os.getenv('DEBUG_GTEST_OUTPUT_TEST'):238 open(os.path.join(239 gtest_test_utils.GetSourceDir(),240 '_gtest_output_test_normalized_actual.txt'), 'wb').write(241 normalized_actual)242 open(os.path.join(243 gtest_test_utils.GetSourceDir(),244 '_gtest_output_test_normalized_golden.txt'), 'wb').write(245 normalized_golden)246 self.assertEqual(normalized_golden, normalized_actual)247if __name__ == '__main__':248 if sys.argv[1:] == [GENGOLDEN_FLAG]:249 if CAN_GENERATE_GOLDEN_FILE:250 output = GetOutputOfAllCommands()251 golden_file = open(GOLDEN_PATH, 'wb')252 golden_file.write(output)253 golden_file.close()254 else:255 message = (256 """Unable to write a golden file when compiled in an environment257that does not support all the required features (death tests, typed tests,258and multiple threads). Please generate the golden file using a binary built259with those features enabled.""")260 sys.stderr.write(message)261 sys.exit(1)262 else:...
test_house_hearing.py
Source:test_house_hearing.py
1import committee_meetings2import lxml.etree3import os4import unittest5import utils6# Parsing the House hearing info7class HearingInfo(unittest.TestCase):8 def test_hearing(self):9 committees = {}10 for c in utils.yaml_load(11 "test/fixtures/committees-current.yaml"):12 committees[c["thomas_id"]] = c13 if "house_committee_id" in c:14 committees[c["house_committee_id"] + "00"] = c15 c["subcommittees"] = dict((s["thomas_id"], s)16 for s in c.get("subcommittees", []))17 hearing_xml = "test/fixtures/hearings/sample_hearing.xml"18 file_xml = open(hearing_xml, "r")19 dom = lxml.etree.parse(file_xml)20 test_output = committee_meetings.parse_house_committee_meeting(21 '102252', dom, [], committees, {"debug": False}, None, ["BILLS-113hr4435ih.pdf", "BILLS-113hr4435ih.xml"])22 23# event_id, dom, existing_meetings, committees, options, witnesses, uploaded_documents24 self.assertEqual(test_output['bill_ids'], ['hr4435-113'])25 self.assertEqual(test_output['chamber'], 'house')26 self.assertEqual(test_output['committee'], 'HSRU')27 self.assertEqual(test_output['congress'], 113)28 self.assertEqual(test_output['house_meeting_type'], 'HMTG')29 self.assertEqual(test_output['meeting_documents'][0][30 'description'], 'H.R. 4435 (as introduced)')31 self.assertEqual(32 test_output['meeting_documents'][0]['bill_id'],33 'hr4435-113')34 self.assertEqual(35 test_output['meeting_documents'][0]['version_code'], 'ih')36 self.assertEqual(test_output['meeting_documents'][0]['type'], 'BR')37 self.assertEqual(38 test_output['meeting_documents'][0]['urls'], [39 {'url': 'http://beta.congress.gov/113/bills/hr4435/BILLS-113hr4435ih.pdf', 'file_found': True}, 40 {'url': 'http://beta.congress.gov/113/bills/hr4435/BILLS-113hr4435ih.xml', 'file_found': True},])41 self.assertEqual(test_output['occurs_at'], '2014-05-19T17:00:00')42 self.assertEqual(test_output['room'], 'CAPITOL H-313')43 self.assertEqual(test_output['subcommittee'], None)44 self.assertEqual(test_output[45 'topic'], u'H.R. 4435\u2014National Defense Authorization Act for Fiscal Year 2015 [General Debate]; H.R. 4660\u2014Commerce, Justice, Science, and Related Agencies Appropriations Act, 2015')46 self.assertEqual(test_output[47 'url'], 'http://docs.house.gov/Committee/Calendar/ByEvent.aspx?EventID=102252')48 def test_witnesses(self):49 witness_xml = "test/fixtures/hearings/sample_witness.xml"50 file_xml = open(witness_xml, "r")51 witness_tree = lxml.etree.parse(file_xml)52 uploaded_documents = ["HHRG-113-GO25-Bio-CochraneJ-20140522.pdf"]53 event_id = "102266"54 test_output = committee_meetings.parse_witness_list(witness_tree, uploaded_documents, event_id)["hearing_witness_info"]55 self.assertEqual(test_output[0]['documents'][0]['type'], 'WB')56 self.assertEqual(57 test_output[0]['documents'][0]['description'], 'Cochrane Bio')58 self.assertEqual(test_output[0]['documents'][0]['urls'], [59 {'url': 'http://docs.house.gov/meetings/GO/GO25/20140522/102266/HHRG-113-GO25-Bio-CochraneJ-20140522.pdf', 'file_found': True}])60 self.assertEqual(test_output[0]['house_event_id'], '102266')61 self.assertEqual(test_output[0]['first_name'], 'James')62 self.assertEqual(test_output[0]['last_name'], 'Cochrane')63 self.assertEqual(64 test_output[0]['position'],65 'Chief Information Officer and Executive Vice President')...
test_psa_mask.py
Source:test_psa_mask.py
1# Copyright (c) OpenMMLab. All rights reserved.2import numpy as np3import torch4import torch.nn as nn5class Loss(nn.Module):6 def __init__(self):7 super().__init__()8 def forward(self, input, target):9 input = input.view(-1)10 target = target.view(-1)11 return torch.mean(input - target)12class TestPSAMask:13 def test_psa_mask_collect(self):14 if not torch.cuda.is_available():15 return16 from mmcv.ops import PSAMask17 test_loss = Loss()18 input = np.fromfile(19 'tests/data/for_psa_mask/psa_input.bin', dtype=np.float32)20 output_collect = np.fromfile(21 'tests/data/for_psa_mask/psa_output_collect.bin', dtype=np.float32)22 input = input.reshape((4, 16, 8, 8))23 output_collect = output_collect.reshape((4, 64, 8, 8))24 label = torch.ones((4, 64, 8, 8))25 input = torch.FloatTensor(input)26 input.requires_grad = True27 psamask_collect = PSAMask('collect', (4, 4))28 # test collect cpu29 test_output = psamask_collect(input)30 loss = test_loss(test_output, label)31 loss.backward()32 test_output = test_output.detach().numpy()33 assert np.allclose(test_output, output_collect)34 assert test_output.shape == output_collect.shape35 psamask_collect.cuda()36 input = input.cuda()37 label = label.cuda()38 # test collect cuda39 test_output = psamask_collect(input)40 loss = test_loss(test_output, label)41 loss.backward()42 test_output = test_output.detach().cpu().numpy()43 assert np.allclose(test_output, output_collect)44 assert test_output.shape == output_collect.shape45 def test_psa_mask_distribute(self):46 if not torch.cuda.is_available():47 return48 from mmcv.ops import PSAMask49 test_loss = Loss()50 input = np.fromfile(51 'tests/data/for_psa_mask/psa_input.bin', dtype=np.float32)52 output_distribute = np.fromfile(53 'tests/data/for_psa_mask/psa_output_distribute.bin',54 dtype=np.float32)55 input = input.reshape((4, 16, 8, 8))56 output_distribute = output_distribute.reshape((4, 64, 8, 8))57 label = torch.ones((4, 64, 8, 8))58 input = torch.FloatTensor(input)59 input.requires_grad = True60 psamask_distribute = PSAMask('distribute', (4, 4))61 # test distribute cpu62 test_output = psamask_distribute(input)63 loss = test_loss(test_output, label)64 loss.backward()65 test_output = test_output.detach().numpy()66 assert np.allclose(test_output, output_distribute)67 assert test_output.shape == output_distribute.shape68 psamask_distribute.cuda()69 input = input.cuda()70 label = label.cuda()71 # test distribute cuda72 test_output = psamask_distribute(input)73 loss = test_loss(test_output, label)74 loss.backward()75 test_output = test_output.detach().cpu().numpy()76 assert np.allclose(test_output, output_distribute)...
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!!