Best Python code snippet using unittest-xml-reporting_python
main.py
Source:main.py
...83 def usageExit(self, msg=None):84 if msg:85 print(msg)86 if self._discovery_parser is None:87 self._initArgParsers()88 self._print_help()89 sys.exit(2)90 def _print_help(self, *args, **kwargs):91 if self.module is None:92 print(self._main_parser.format_help())93 print(MAIN_EXAMPLES % {'prog': self.progName})94 self._discovery_parser.print_help()95 else:96 print(self._main_parser.format_help())97 print(MODULE_EXAMPLES % {'prog': self.progName})98 def parseArgs(self, argv):99 self._initArgParsers()100 if self.module is None:101 if len(argv) > 1 and argv[1].lower() == 'discover':102 self._do_discovery(argv[2:])103 return104 self._main_parser.parse_args(argv[1:], self)105 if not self.tests:106 # this allows "python -m unittest -v" to still work for107 # test discovery.108 self._do_discovery([])109 return110 else:111 self._main_parser.parse_args(argv[1:], self)112 if self.tests:113 self.testNames = _convert_names(self.tests)114 if __name__ == '__main__':115 # to support python -m unittest ...116 self.module = None117 elif self.defaultTest is None:118 # createTests will load tests from self.module119 self.testNames = None120 elif isinstance(self.defaultTest, str):121 self.testNames = (self.defaultTest,)122 else:123 self.testNames = list(self.defaultTest)124 self.createTests()125 def createTests(self):126 if self.testNames is None:127 self.test = self.testLoader.loadTestsFromModule(self.module)128 else:129 self.test = self.testLoader.loadTestsFromNames(self.testNames,130 self.module)131 def _initArgParsers(self):132 parent_parser = self._getParentArgParser()133 self._main_parser = self._getMainArgParser(parent_parser)134 self._discovery_parser = self._getDiscoveryArgParser(parent_parser)135 def _getParentArgParser(self):136 parser = argparse.ArgumentParser(add_help=False)137 parser.add_argument('-v', '--verbose', dest='verbosity',138 action='store_const', const=2,139 help='Verbose output')140 parser.add_argument('-q', '--quiet', dest='verbosity',141 action='store_const', const=0,142 help='Quiet output')143 if self.failfast is None:144 parser.add_argument('-f', '--failfast', dest='failfast',145 action='store_true',146 help='Stop on first fail or error')147 self.failfast = False148 if self.catchbreak is None:149 parser.add_argument('-c', '--catch', dest='catchbreak',150 action='store_true',151 help='Catch ctrl-C and display results so far')152 self.catchbreak = False153 if self.buffer is None:154 parser.add_argument('-b', '--buffer', dest='buffer',155 action='store_true',156 help='Buffer stdout and stderr during tests')157 self.buffer = False158 return parser159 def _getMainArgParser(self, parent):160 parser = argparse.ArgumentParser(parents=[parent])161 parser.prog = self.progName162 parser.print_help = self._print_help163 parser.add_argument('tests', nargs='*',164 help='a list of any number of test modules, '165 'classes and test methods.')166 return parser167 def _getDiscoveryArgParser(self, parent):168 parser = argparse.ArgumentParser(parents=[parent])169 parser.prog = '%s discover' % self.progName170 parser.epilog = ('For test discovery all test modules must be '171 'importable from the top level directory of the '172 'project.')173 parser.add_argument('-s', '--start-directory', dest='start',174 help="Directory to start discovery ('.' default)")175 parser.add_argument('-p', '--pattern', dest='pattern',176 help="Pattern to match tests ('test*.py' default)")177 parser.add_argument('-t', '--top-level-directory', dest='top',178 help='Top level directory of project (defaults to '179 'start directory)')180 for arg in ('start', 'pattern', 'top'):181 parser.add_argument(arg, nargs='?',182 default=argparse.SUPPRESS,183 help=argparse.SUPPRESS)184 return parser185 def _do_discovery(self, argv, Loader=None):186 self.start = '.'187 self.pattern = 'test*.py'188 self.top = None189 if argv is not None:190 # handle command line args for test discovery191 if self._discovery_parser is None:192 # for testing193 self._initArgParsers()194 self._discovery_parser.parse_args(argv, self)195 loader = self.testLoader if Loader is None else Loader()196 self.test = loader.discover(self.start, self.pattern, self.top)197 def runTests(self):198 if self.catchbreak:199 installHandler()200 if self.testRunner is None:201 self.testRunner = runner.TextTestRunner202 if isinstance(self.testRunner, type):203 try:204 testRunner = self.testRunner(verbosity=self.verbosity,205 failfast=self.failfast,206 buffer=self.buffer,207 warnings=self.warnings)...
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!!