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

Community
  • 1
  • 1
Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37

1 Answers1

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')]
    >>> 

    """
Tyler Crompton
  • 12,284
  • 14
  • 65
  • 94
  • The standard interpretation of docstrings is to ignore leading whitespace in this fashion. I'm rather surprised that doctest doesn't do that. – Kevin Mar 19 '16 at 06:44
  • Agreed but isn't there anything which looks pleasing to the eyes? – Tasdik Rahman Mar 19 '16 at 07: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. – Tyler Crompton Mar 21 '16 at 21:36