Best Python code snippet using autotest_python
test.py
Source:test.py  
...294            _validate_args(args, dargs, self.initialize, self.setup,295                           self.execute, self.cleanup)296            try:297                # Initialize:298                _cherry_pick_call(self.initialize, *args, **dargs)299                lockfile = open(os.path.join(self.job.tmpdir, '.testlock'), 'w')300                try:301                    fcntl.flock(lockfile, fcntl.LOCK_EX)302                    # Setup: (compile and install the test, if needed)303                    p_args, p_dargs = _cherry_pick_args(self.setup,args,dargs)304                    utils.update_version(self.srcdir, self.preserve_srcdir,305                                         self.version, self.setup,306                                         *p_args, **p_dargs)307                finally:308                    fcntl.flock(lockfile, fcntl.LOCK_UN)309                    lockfile.close()310                # Execute:311                os.chdir(self.outputdir)312                # call self.warmup cherry picking the arguments it accepts and313                # translate exceptions if needed314                _call_test_function(_cherry_pick_call, self.warmup,315                                    *args, **dargs)316                if hasattr(self, 'run_once'):317                    p_args, p_dargs = _cherry_pick_args(self.run_once,318                                                        args, dargs)319                    # pull in any non-* and non-** args from self.execute320                    for param in _get_nonstar_args(self.execute):321                        if param in dargs:322                            p_dargs[param] = dargs[param]323                else:324                    p_args, p_dargs = _cherry_pick_args(self.execute,325                                                        args, dargs)326                _call_test_function(self.execute, *p_args, **p_dargs)327            except Exception:328                try:329                    logging.exception('Exception escaping from test:')330                except:331                    pass # don't let logging exceptions here interfere332                # Save the exception while we run our cleanup() before333                # reraising it.334                exc_info = sys.exc_info()335                try:336                    try:337                        if run_cleanup:338                            _cherry_pick_call(self.cleanup, *args, **dargs)339                    except Exception:340                        print 'Ignoring exception during cleanup() phase:'341                        traceback.print_exc()342                        print 'Now raising the earlier %s error' % exc_info[0]343                    self.crash_handler_report()344                finally:345                    self.job.logging.restore()346                    try:347                        raise exc_info[0], exc_info[1], exc_info[2]348                    finally:349                        # http://docs.python.org/library/sys.html#sys.exc_info350                        # Be nice and prevent a circular reference.351                        del exc_info352            else:353                try:354                    if run_cleanup:355                        _cherry_pick_call(self.cleanup, *args, **dargs)356                    self.crash_handler_report()357                finally:358                    self.job.logging.restore()359        except error.AutotestError:360            if self.network_destabilizing:361                self.job.enable_warnings("NETWORK")362            # Pass already-categorized errors on up.363            raise364        except Exception, e:365            if self.network_destabilizing:366                self.job.enable_warnings("NETWORK")367            # Anything else is an ERROR in our own code, not execute().368            raise error.UnhandledTestError(e)369        else:370            if self.network_destabilizing:371                self.job.enable_warnings("NETWORK")372def _get_nonstar_args(func):373    """Extract all the (normal) function parameter names.374    Given a function, returns a tuple of parameter names, specifically375    excluding the * and ** parameters, if the function accepts them.376    @param func: A callable that we want to chose arguments for.377    @return: A tuple of parameters accepted by the function.378    """379    return func.func_code.co_varnames[:func.func_code.co_argcount]380def _cherry_pick_args(func, args, dargs):381    """Sanitize positional and keyword arguments before calling a function.382    Given a callable (func), an argument tuple and a dictionary of keyword383    arguments, pick only those arguments which the function is prepared to384    accept and return a new argument tuple and keyword argument dictionary.385    Args:386      func: A callable that we want to choose arguments for.387      args: A tuple of positional arguments to consider passing to func.388      dargs: A dictionary of keyword arguments to consider passing to func.389    Returns:390      A tuple of: (args tuple, keyword arguments dictionary)391    """392    # Cherry pick args:393    if func.func_code.co_flags & 0x04:394        # func accepts *args, so return the entire args.395        p_args = args396    else:397        p_args = ()398    # Cherry pick dargs:399    if func.func_code.co_flags & 0x08:400        # func accepts **dargs, so return the entire dargs.401        p_dargs = dargs402    else:403        # Only return the keyword arguments that func accepts.404        p_dargs = {}405        for param in _get_nonstar_args(func):406            if param in dargs:407                p_dargs[param] = dargs[param]408    return p_args, p_dargs409def _cherry_pick_call(func, *args, **dargs):410    """Cherry picks arguments from args/dargs based on what "func" accepts411    and calls the function with the picked arguments."""412    p_args, p_dargs = _cherry_pick_args(func, args, dargs)413    return func(*p_args, **p_dargs)414def _validate_args(args, dargs, *funcs):415    """Verify that arguments are appropriate for at least one callable.416    Given a list of callables as additional parameters, verify that417    the proposed keyword arguments in dargs will each be accepted by at least418    one of the callables.419    NOTE: args is currently not supported and must be empty.420    Args:421      args: A tuple of proposed positional arguments.422      dargs: A dictionary of proposed keyword arguments.423      *funcs: Callables to be searched for acceptance of args and dargs....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!!
