我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用doctest.DocTestCase()。
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( PurchaseTestCase)) suite.addTests(doctest.DocFileSuite('scenario_purchase.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='UTF-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase(StockTestCase)) suite.addTests(doctest.DocFileSuite('scenario_stock_shipment_out.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) suite.addTests(doctest.DocFileSuite( 'scenario_stock_average_cost_price.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) suite.addTests(doctest.DocFileSuite( 'scenario_stock_inventory.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) suite.addTests(doctest.DocFileSuite('scenario_stock_shipment_internal.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) suite.addTests(doctest.DocFileSuite('scenario_stock_reporting.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) return suite
def setUp(self): """Modified test setup that syncs with ipython namespace""" #print "setUp test", self._dt_test.examples # dbg if isinstance(self._dt_test.examples[0], IPExample): # for IPython examples *only*, we swap the globals with the ipython # namespace, after updating it with the globals (which doctest # fills with the necessary info from the module being tested). self.user_ns_orig = {} self.user_ns_orig.update(_ip.user_ns) _ip.user_ns.update(self._dt_test.globs) # We must remove the _ key in the namespace, so that Python's # doctest code sets it naturally _ip.user_ns.pop('_', None) _ip.user_ns['__builtins__'] = builtin_mod self._dt_test.globs = _ip.user_ns super(DocTestCase, self).setUp()
def makeTest(self, obj, parent): """Look for doctests in the given object, which will be a function, method or class. """ #print 'Plugin analyzing:', obj, parent # dbg # always use whitespace and ellipsis options optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS doctests = self.finder.find(obj, module=getmodule(parent)) if doctests: for test in doctests: if len(test.examples) == 0: continue yield DocTestCase(test, obj=obj, optionflags=optionflags, checker=self.checker)
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( StockSupplyTestCase)) suite.addTests(doctest.DocFileSuite('scenario_stock_internal_supply.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) suite.addTests(doctest.DocFileSuite( 'scenario_stock_supply_purchase_request.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( StockLotTestCase)) suite.addTests(doctest.DocFileSuite('scenario_stock_lot_shipment_out.rst', setUp=doctest_setup, tearDown=doctest_teardown, encoding='utf-8', optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( AccountProductTestCase)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.currency.tests import test_currency for test in test_currency.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( CompanyTestCase)) return suite
def loadTestsFromModule(self, module): """Load doctests from the module. """ log.debug("loading from %s", module) if not self.matches(module.__name__): log.debug("Doctest doesn't want module %s", module) return try: tests = self.finder.find(module) except AttributeError: log.exception("Attribute error loading from %s", module) # nose allows module.__test__ = False; doctest does not and throws # AttributeError return if not tests: log.debug("No tests found in %s", module) return tests.sort() module_file = src(module.__file__) # FIXME this breaks the id plugin somehow (tests probably don't # get wrapped in result proxy or something) cases = [] for test in tests: if not test.examples: continue if not test.filename: test.filename = module_file cases.append(DocTestCase(test, optionflags=self.optionflags, result_var=self.doctest_result_var)) if cases: yield self.suiteClass(cases, context=module, can_split=False)
def makeTest(self, obj, parent): """Look for doctests in the given object, which will be a function, method or class. """ name = getattr(obj, '__name__', 'Unnammed %s' % type(obj)) doctests = self.finder.find(obj, module=getmodule(parent), name=name) if doctests: for test in doctests: if len(test.examples) == 0: continue yield DocTestCase(test, obj=obj, optionflags=self.optionflags, result_var=self.doctest_result_var)
def __init__(self, test, optionflags=0, setUp=None, tearDown=None, checker=None, obj=None, result_var='_'): self._result_var = result_var self._nose_obj = obj super(DocTestCase, self).__init__( test, optionflags=optionflags, setUp=setUp, tearDown=tearDown, checker=checker)
def setUp(self): if self._result_var is not None: self._old_displayhook = sys.displayhook sys.displayhook = self._displayhook super(DocTestCase, self).setUp()
def tearDown(self): super(DocTestCase, self).tearDown() if self._result_var is not None: sys.displayhook = self._old_displayhook delattr(builtin_mod, self._result_var)
def check_output(self, want, got, optionflags): ret = doctest.OutputChecker.check_output(self, want, got, optionflags) if not ret: if "#random" in want: return True # it would be useful to normalize endianness so that # bigendian machines don't fail all the tests (and there are # actually some bigendian examples in the doctests). Let's try # making them all little endian got = got.replace("'>", "'<") want = want.replace("'>", "'<") # try to normalize out 32 and 64 bit default int sizes for sz in [4, 8]: got = got.replace("'<i%d'" % sz, "int") want = want.replace("'<i%d'" % sz, "int") ret = doctest.OutputChecker.check_output(self, want, got, optionflags) return ret # Subclass nose.plugins.doctests.DocTestCase to work around a bug in # its constructor that blocks non-default arguments from being passed # down into doctest.DocTestCase
def __init__(self, test, optionflags=0, setUp=None, tearDown=None, checker=None, obj=None, result_var='_'): self._result_var = result_var self._nose_obj = obj doctest.DocTestCase.__init__(self, test, optionflags=optionflags, setUp=setUp, tearDown=tearDown, checker=checker)
def make_doctest(filename): filename = _get_caller_relative_path(filename) doctests = read_file(filename) doctests = _fix_unicode(r'\1\2', doctests) doctests = _fix_exceptions(r'\1 as \2', doctests) return doctest.DocTestCase( doctest_parser.get_doctest( doctests, {}, os.path.basename(filename), filename, 0))
def make_doctest(filename): filename = _get_caller_relative_path(filename) doctests = read_file(filename) doctests = _fix_traceback(r'\1\2', doctests) doctests = _fix_exceptions(r'\1, \2', doctests) doctests = _fix_bytes(r'\1\2', doctests) return doctest.DocTestCase( doctest_parser.get_doctest( doctests, {}, os.path.basename(filename), filename, 0))
def __init__(self, test, optionflags=0, setUp=None, tearDown=None, checker=None, obj=None, result_var='_'): self._result_var = result_var doctests.DocTestCase.__init__(self, test, optionflags=optionflags, setUp=setUp, tearDown=tearDown, checker=checker) # Now we must actually copy the original constructor from the stdlib # doctest class, because we can't call it directly and a bug in nose # means it never gets passed the right arguments. self._dt_optionflags = optionflags self._dt_checker = checker self._dt_test = test self._dt_test_globs_ori = test.globs self._dt_setUp = setUp self._dt_tearDown = tearDown # XXX - store this runner once in the object! runner = IPDocTestRunner(optionflags=optionflags, checker=checker, verbose=False) self._dt_runner = runner # Each doctest should remember the directory it was loaded from, so # things like %run work without too many contortions self._ori_dir = os.path.dirname(test.filename) # Modified runTest from the default stdlib
def tearDown(self): # Undo the test.globs reassignment we made, so that the parent class # teardown doesn't destroy the ipython namespace if isinstance(self._dt_test.examples[0], IPExample): self._dt_test.globs = self._dt_test_globs_ori _ip.user_ns.clear() _ip.user_ns.update(self.user_ns_orig) # XXX - fperez: I am not sure if this is truly a bug in nose 0.11, but # it does look like one to me: its tearDown method tries to run # # delattr(builtin_mod, self._result_var) # # without checking that the attribute really is there; it implicitly # assumes it should have been set via displayhook. But if the # displayhook was never called, this doesn't necessarily happen. I # haven't been able to find a little self-contained example outside of # ipython that would show the problem so I can report it to the nose # team, but it does happen a lot in our code. # # So here, we just protect as narrowly as possible by trapping an # attribute error whose message would be the name of self._result_var, # and letting any other error propagate. try: super(DocTestCase, self).tearDown() except AttributeError as exc: if exc.args[0] != self._result_var: raise # A simple subclassing of the original with a different class name, so we can # distinguish and treat differently IPython examples from pure python ones.
def loadTestsFromModule(self, module): #print '*** ipdoctest - lTM',module # dbg if not self.matches(module.__name__): log.debug("Doctest doesn't want module %s", module) return tests = self.finder.find(module,globs=self.globs, extraglobs=self.extraglobs) if not tests: return # always use whitespace and ellipsis options optionflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS tests.sort() module_file = module.__file__ if module_file[-4:] in ('.pyc', '.pyo'): module_file = module_file[:-1] for test in tests: if not test.examples: continue if not test.filename: test.filename = module_file yield DocTestCase(test, optionflags=optionflags, checker=self.checker)
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( StockSupplyDayTestCase)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company from trytond.modules.account.tests import test_account for test in chain(test_company.suite(), test_account.suite()): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( AccountPaymentSepaTestCase)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.company.tests import test_company for test in test_company.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( TimesheetCostTestCase)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( AccountCreditLimitTestCase)) return suite
def suite(): suite = trytond.tests.test_tryton.suite() from trytond.modules.account.tests import test_account for test in test_account.suite(): if test not in suite and not isinstance(test, doctest.DocTestCase): suite.addTest(test) suite.addTests(unittest.TestLoader().loadTestsFromTestCase( SaleCreditLimitTestCase)) return suite