[docs]defrun(tests=(),reporter=None,stop_after=None):""" Run the tests that are loaded by each of the strings provided. Arguments: tests (collections.abc.Iterable): the collection of tests (specified as `str` s) to run reporter (twisted.trial.itrial.IReporter): a reporter to use for the run. If unprovided, the default is to return a `virtue.reporters.Counter` (which produces no output). stop_after (int): a number of non-successful tests to allow before stopping the run. """ifreporterisNone:reporter=Counter()ifstop_afterisnotNone:reporter=_StopAfterWrapper(reporter=reporter,limit=stop_after)locator=ObjectLocator()cases=(casefortestintestsforloaderinlocator.locate_by_name(name=test)forcaseinloader.load())suite=unittest.TestSuite(cases)getattr(reporter,"startTestRun",lambda:None)()withwarnings.catch_warnings():warnings.simplefilter("error")suite.run(reporter)getattr(reporter,"stopTestRun",lambda:None)()returnreporter
@attr.s(eq=False)class_StopAfterWrapper:# noqa: PLW1641""" Wrap a reporter to stop after a specified number of non-successes. """_limit=attr.ib()_reporter=attr.ib()_seen=attr.ib(default=0)def__eq__(self,other):returnself._reporter==otherdef__ne__(self,other):returnnotself==otherdef__getattr__(self,attr):returngetattr(self._reporter,attr)defaddError(self,*args,**kwargs):self._see_nonsuccess()self._reporter.addError(*args,**kwargs)defaddFailure(self,*args,**kwargs):self._see_nonsuccess()self._reporter.addFailure(*args,**kwargs)defaddUnexpectedSuccess(self,*args,**kwargs):self._see_nonsuccess()self._reporter.addUnexpectedSuccess(*args,**kwargs)def_see_nonsuccess(self):self._seen+=1ifself._seen==self._limit:self.shouldStop=True