【问题标题】:Keep lines inside docstrings within 79 character limit将文档字符串中的行保持在 79 个字符以内
【发布时间】:2016-03-19 04:48:57
【问题描述】:

我正在我的模块中编写一些文档测试。

相关代码

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

我面临的问题

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

参考文献

我查看了这些,但找不到相关的内容

【问题讨论】:

    标签: python unit-testing docstring doctest


    【解决方案1】:

    尝试删除多余的空格。

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

    【讨论】:

    • 文档字符串的标准解释是以这种方式忽略前导空格。我很惊讶 doctest 没有这样做。
    • 同意,但没有什么好看的吗?
    • 问题是 OP 在字符串中使用了续行符。因此,它们不被视为新行,因此不受空白忽略规则的约束。
    猜你喜欢
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多