0

I am writing some doctests in my module.

Relevant code

def foo():
    """
    Populates the database with 'VALUES'

    >>> import sqlite3
    >>> con = sqlite3.connect('test.db')
    >>> cur = con.cursor()
    >>> cur.execute('select * from users').fetchall()
    [('admin', 'Admin', 1, 'admin123'), \
    ('foo', 'bar', 2, 'foo123'), \
    ('john', 'doe', 3, 'john123')]
    >>> 

    """

    try:
        con = sqlite3.connect('test.db')
        cursor = con.cursor()
        cursor.executemany("INSERT INTO users VALUES (?, ?, ?, ?)", VALUES)
        connection.commit()
        connection.close()
    except sqlite3.OperationalError as msg:
        return msg

Problem that I am facing

$ python -m doctest test_db.py
Failed example:
    cur.execute('select * from users').fetchall()
Expected:
    [('admin', 'Admin', 1, 'admin123'),     ('foo', 'bar', 2, 'foo123'),     ('john', 'doe', 3, 'john123')]
Got:
    [('admin', 'Admin', 1, 'admin123'), ('foo', 'bar', 2, 'foo123'), ('john', 'doe', 3, 'john123')]
**********************************************************************

References

I looked into these but couldn't find something relevant

1 Answer 1

1

Try removing the extra whitespace.

def foo():
    """
    Populates the database with 'VALUES'

    >>> import sqlite3
    >>> con = sqlite3.connect('test.db')
    >>> cur = con.cursor()
    >>> cur.execute('select * from users').fetchall()
    [('admin', 'Admin', 1, 'admin123'), \
('foo', 'bar', 2, 'foo123'), \
('john', 'doe', 3, 'john123')]
    >>> 

    """
3
  • The standard interpretation of docstrings is to ignore leading whitespace in this fashion. I'm rather surprised that doctest doesn't do that.
    – Kevin
    Commented Mar 19, 2016 at 6:44
  • Agreed but isn't there anything which looks pleasing to the eyes? Commented Mar 19, 2016 at 7:48
  • The problem is that OP is using the line continuation character in a string. Therefore, they aren't considered new lines and therefore aren't subjected to the whitespace ignorance rule. Commented Mar 21, 2016 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.