top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Manually build a unittest/doctest object in Python?

+2 votes
194 views

If I have a string that is python code, for example

mycode = "print('hello world')"
myresult = "hello world"

How can a "manually" build a unittest (doctest) and test I get myresult

I have attempted to build a doctest but that is not working.

e = doctest.Example(source="print('hello world')/n", want="hello worldn")
t = doctest.DocTestRunner()
t.run(e)
posted Dec 8, 2015 by Deepak Dasgupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Not easy. Effectively, you would have to re-invent the doctest module and re-engineer it to accept a completely different format.

But if you are willing to write your tests in doctest format, this might help you:

import doctest

def rundoctests(text, name='', globs=None, verbose=None,
 report=True, optionflags=0, extraglobs=None,
 raise_on_error=False,
 quiet=False,):
 # Assemble the globals.
 if globs is None:
 globs = globals()
 globs = globs.copy()
 if extraglobs is not None:
 globs.update(extraglobs)
 if '__name__' not in globs:
 globs['__name__'] = '__main__'
 # Parse the text looking for doc tests.
 parser = doctest.DocTestParser()
 test = parser.get_doctest(text, globs, name, name, 0)
 # Run the tests.
 if raise_on_error:
 runner = doctest.DebugRunner(
 verbose=verbose, optionflags=optionflags)
 else:
 runner = doctest.DocTestRunner(
 verbose=verbose, optionflags=optionflags)
 if quiet:
 runner.run(test, out=lambda s: None)
 else:
 runner.run(test)
 if report:
 runner.summarize()
 # Return a (named, if possible) tuple (failed, attempted).
 a, b = runner.failures, runner.tries
 try:
 TestResults = doctest.TestResults
 except AttributeError:
 return (a, b)
 return TestResults(a, b)

Then call rundoctests(text) to run any doc tests in text. By default, if there are no errors, it prints nothing. If there are errors, it prints the failing tests. Either way, it returns a tuple

(number of failures, number of tests run)

Examples in use:

py> good_code = """
... >>> import math
... >>> print "Hello World!"
... Hello World!
... >>> math.sqrt(100)
... 10.0
... 
... """
py> rundoctests(good_code)
TestResults(failed=0, attempted=3)

py> bad_code = """
... >>> print 10
... 11
... """
py> rundoctests(bad_code)
**********************************************************************
File "", line 2, in 
Failed example:
 print 10
Expected:
 11
Got:
 10
**********************************************************************
1 items had failures:
 1 of 1 in 
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
answer Dec 8, 2015 by Satish Mishra
Similar Questions
+1 vote

To loop though an iterator one usually uses a higher-level construct such as a 'for' loop. However, if you want to step through it manually you can do so with next(iter).

I expected the same functionality with the new 'asynchronous iterator' in Python 3.5, but I cannot find it.

I can achieve the desired result by calling 'await aiter.__anext__()', but this is clunky.

Am I missing something?

+1 vote

I understand there have been changes to the way that extensions are supposed to be built for windows. Is there any user documentation regarding these changes?

Last time I looked the appropriate Visual Studio hadn't been release so I guess I will need to use my MSDN skills (or incantations) to first get that installed.

0 votes

On building Python 2.7.5 I got the following message:

Python build finished, but the necessary bits to build these modules were not found:
dl imageop linuxaudiodev 
spwd sunaudiodev 
To find the necessary bits, look in setup.py in detect_modules()  for the module's name.

It carried on with the installation OK, but I don't understand the last sentence in the message. How can I find out exactly what modules are missing, and what I need to do to make sure they are built next time?

0 votes

I need to print the Dictionary in a detailed way.

Current When am print the dictionary am getting like below.

<Details.Value: object at 0x0000027062245160>
...