doctest

python doctest: expected result is the same as the “got” result but the test failed

允我心安 提交于 2020-01-11 08:25:12
问题 I am on a learning stage of using python as a tool for software QA. I wrote the next simple test in order to find the letter 'a' in a text file number matrix. problem is that the test fails even though the expect equals to what i got. Why is that? Can you tell me what am I doing wrong? test script: fin = open("abc.txt", "r") arr_fin = [] for line in fin: arr_fin.append(line.split()) print arr_fin for row in arr_fin: arr_fin_1 = " ".join('{0:4}'.format(i or " ") for i in row) print arr_fin_1

Python doctest failed for equal “Expected” and “Got”

感情迁移 提交于 2020-01-07 04:18:06
问题 I try to use doctest for my scenario: def cycle(*its): """Return the cyclic iterator. >>> c = cycle([1,2,3]) >>> [next(c) for i in range(10)] [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] """ saved = [] for it in its: for el in it: saved.append(el) yield el while True: for el in saved: yield el if __name__ == "__main__": import doctest doctest.testmod() But I get Failed in out: Trying: c = cycle([1,2,3]) Expecting nothing ok Trying: [next(c) for i in range(10)] Expecting: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] ****

Using Mocks inside Doctests?

吃可爱长大的小学妹 提交于 2020-01-02 03:45:09
问题 I am using doctests. I am wondering what is the correct way to doctest a function that performs an external action (e.g. sends email, connects to a server, etc)? Using Mock seems like the answer but it will muddy up the doc string of the function. For example: class SSHConnection(BaseConnection): """Provides basic SSH functions. >>> host = '127.0.0.1' >>> port = 22 >>> username = 'user' >>> password = 'password' >>> ssh = SSHConnection(host, username, password, port) >>> ssh.execute('uname -a

Examples of using Doctests in Django in an Agile / BDD way

眉间皱痕 提交于 2020-01-01 05:20:51
问题 I'm interested in learning how to Doctests and Unit tests in a more Agile / BDD way. I've found a few tutorials that seem reasonable, but they are just thumbnails. What I would really like to see is the source code of some Django projects that were developed BDD style. The things I'm unclear about are how do you handle request objects etc. I have a situation where I have deployed my app and I'm getting completely different behavior in production that I did in development or even from the

unicode_literals and doctest in Python 2.7 AND Python 3.5

半城伤御伤魂 提交于 2019-12-30 23:04:34
问题 Consider the following demo script: # -*- coding: utf-8 -*- from __future__ import division from __future__ import unicode_literals def myDivi(): """ This is a small demo that just returns the output of a divison. >>> myDivi() 0.5 """ return 1/2 def myUnic(): """ This is a small demo that just returns a string. >>> myUnic() 'abc' """ return 'abc' if __name__ == "__main__": import doctest extraglobs = {} doctest.testmod(extraglobs=extraglobs) The doctest passes on Python 3.5, but fails on

How to make py.test run doctests as well as normal tests directory?

我们两清 提交于 2019-12-30 03:47:09
问题 We currently have py.test with the coverage plugin running over our tests in a tests directory. What's the simplest way to also run doctests extracted from our main code? --doctest-modules doesn't work (probably since it just runs doctests from tests ). Note that we want to include doctests in the same process (and not simply run a separate invocation of py.test ) because we want to account for doctest in code coverage. 回答1: Now it is implemented :-). To use, either run py.test --doctest

Doctest failing inspite of having correct output

♀尐吖头ヾ 提交于 2019-12-24 22:54:07
问题 My function is def validate_latitude(lat): """Enforce latitude is in range >>> validate_latitude(65) 65 >>> validate_latitude(91) 90 >>> validate_latitude(-91) -90 """ lat = min(lat, 90) lat = max(lat, -90) return lat And the test fails with this output ********************************************************************** File "packages/utils.py", line 64, in __main__.validate_latitude Failed example: validate_latitude(-91) Expected: -90 Got: -90 *********************************************

Python doctest execution context

不羁岁月 提交于 2019-12-24 10:41:19
问题 I have the following function which I do unit testing with doctest. from collections import deque def fill_q(histq=deque([])): """ >>> fill_q() deque([1, 2, 3]) >>> fill_q() deque([1, 2, 3]) """ if histq: assert(len(histq) == 0) histq.append(1) histq.append(2) histq.append(3) return histq if __name__ == "__main__": import doctest doctest.testmod() the first case passes, but the second call to fill_q fails, yet it's the same code: ***************************************************************

How can I make doctests triggered by pytest ignore unicode prefix `u'…'` of strings?

与世无争的帅哥 提交于 2019-12-23 21:07:23
问题 I want my code to work in Python 2 and 3. I use doctests and from __future__ import unicode_literals Is there a flag I can set / a plugin which makes it ignore that Python 2 has the u prefix for unicode strings? Example One test that works in Python 3, but fails in Python 2: Expected: 'Me \\& you.' Got: u'Me \\& you.' Minimal example from __future__ import unicode_literals def foo(): """ Returns ------- unicode - for Python 2 and Python 3 Examples -------- >>> foo() 'bar' """ return 'bar' if

Doctest Involving Escape Characters

不羁岁月 提交于 2019-12-23 08:04:36
问题 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