doctest

Doctest Involving Escape Characters

对着背影说爱祢 提交于 2019-12-23 08:04:15
问题 Have a function fix(), as a helper function to an output function which writes strings to a text file. def fix(line): """ returns the corrected line, with all apostrophes prefixed by an escape character >>> fix('DOUG\'S') 'DOUG\\\'S' """ if '\'' in line: return line.replace('\'', '\\\'') return line Turning on doctests, I get the following error: Failed example: fix('DOUG'S') Exception raised: Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.7

Mock Y of (from X import Y) in doctest (python)

混江龙づ霸主 提交于 2019-12-22 10:59:49
问题 I'm trying to create a doctest with mock of function that resides in a separate module and that is imported as bellow from foomodule import foo def bar(): """ >>> from minimock import mock >>> mock('foo', nsdicts=(bar.func_globals,), returns=5) >>> bar() Called foo() 10 """ return foo() * 2 import doctest doctest.testmod() foomodule.py: def foo(): raise ValueError, "Don't call me during testing!" This fails. If I change import to import foomodule and use foomodule.foo everywhere Then it works

How come there's no C# equivalent of python's doctest feature?

冷暖自知 提交于 2019-12-22 06:39:06
问题 Seems like it would be a good way to introduce some people to unit testing. 回答1: Well for one thing, the documentation for doctest talks about "interactive Python sessions". There's no equivalent of that in C#... so how would the output be represented? How would you perform all the necessary setup? I dare say such a thing would be possible, but personally I think that at least for C#, it's clearer to have unit tests as unit tests , where you have all the benefits of the fact that you're

Python doctest for shell scripts that test argument parsing without polluting docstring with os.popen()

混江龙づ霸主 提交于 2019-12-21 10:17:14
问题 Is there a way to write a python doctest string to test a script intended to be launched from the command line (terminal) that doesn't pollute the documentation examples with os.popen calls? #!/usr/bin/env python # filename: add """ Example: >>> import os >>> os.popen('add -n 1 2').read().strip() '3' """ if __name__ == '__main__': from argparse import ArgumentParser p = ArgumentParser(description=__doc__.strip()) p.add_argument('-n',type = int, nargs = 2, default = 0,help = 'Numbers to add.')

Python doctest: skip a test conditionally

心不动则不痛 提交于 2019-12-21 03:52:08
问题 I know how to skip a doctest using # doctest: +SKIP , but I can't figure out how to skip a test sometimes , based on a runtime condition. For example: >>> if os.path.isfile("foo"): ... open("foo").readlines() ... else: ... pass # doctest: +SKIP ['hello', 'world'] That's the sort of thing I want to do. I would also accept a solution which runs the test, but changes the expected result to an exception with traceback if the condition is not met (i.e. run the test unconditionally but modify the

Doctests that contain string literals

耗尽温柔 提交于 2019-12-21 02:47:30
问题 I have a unit test that I'd like to write for a function that takes XML as a string. It's a doctest and I'd like the XML in-line with the tests. Since the XML is multi-line, I tried a string literal within the doctest, but no success. Here's simplified test code: def test(): """ >>> config = \"\"\"\ <?xml version="1.0"?> <test> <data>d1</data> <data>d2</data> </test>\"\"\" """ if __name__ == "__main__": import doctest doctest.testmod(name='test') The error I get is File "<doctest test.test[0]

Mocking ImportError in Python

坚强是说给别人听的谎言 提交于 2019-12-17 16:29:41
问题 I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback actions <-- this one is easy with mocking # zope.component.queryUtility to raise a NameError Any ideas? EDIT: Alex's suggestion doesn't seem to work: >>> import __builtin__ >>>

object reuse in python doctest

不问归期 提交于 2019-12-12 10:37:46
问题 I have a sample doctest like this one. """ This is the "iniFileGenerator" module. >>> hintFile = "./tests/unit_test_files/hint.txt" >>> f = iniFileGenerator(hintFile) >>> print f.hintFilePath ./tests/unit_test_files/hint.txt """ class iniFileGenerator: def __init__(self, hintFilePath): self.hintFilePath = hintFilePath def hello(self): """ >>> f.hello() hello """ print "hello" if __name__ == "__main__": import doctest doctest.testmod() When I execute this code, I got this error. Failed example

Configure Django to find all doctests in all modules?

≡放荡痞女 提交于 2019-12-12 07:48:35
问题 If I run the following command: >python manage.py test Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so: #tests.py from myapp.module1 import _function1, _function2 __test__ = { "_function1": _function1, "_function2": _function2 } If I want to include more doctests, is there an easier way than enumerating them all in this dictionary?

Doctest of function to make a list of squares

天大地大妈咪最大 提交于 2019-12-12 01:58:55
问题 I am trying to define a function to return squares for integers within a given range: #this is my code def squares(start, end): """ Given the starting and ending numbers, return a list of the squares of the numbers from start to end. >>>squares(1, 5) [1, 4, 9, 16, 25] >>>squares(2, 4) [4, 9, 16] >>>squares(0, 1) [0, 1] >>>squares(0, 2) [0, 1, 4] """ return [i**2 for i in range(start, end+1)] if __name__ == "__main__": import doctest doctest.testmod(verbose=True, optionflags=doctest.NORMALIZE