将文档字符串中的行保持在79个字符的限制内

时间:2016-03-19 04:48:57

标签: python unit-testing docstring doctest

我正在我的模块中写一些doctests。

相关代码

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

参考

我调查了这些,但找不到相关的东西

1 个答案:

答案 0 :(得分:1)

尝试删除额外的空格。

// global arrays that we want to assign to asynchronously
var array1 = [String]()
var array2 = [String]()
var array3 = [String]()

// kick everything off
loadAsyncContent()

func loadAsyncContent() {

    // function to handle the query result strings
    // note that outputArray is an inout parameter that will be a reference to one of our global arrays
    func resultsCallbackHandler(results: [String], inout outputArray: [String]) {

        // assign the results to the specified array
        outputArray = results

        // trigger some action every time a query returns it's strings
        reloadMyView() 
    }

    // kick off each query by telling it which database table to query and
    // we're also giving each call a function to run along with a reference to which array the results should be assigned to
    queryTable("Table1") {(results: [String]) -> Void in resultsCallbackHandler(results, outputArray: &self.array1)}
    queryTable("Table2") {(results: [String]) -> Void in resultsCallbackHandler(results, outputArray: &self.array2)}
    queryTable("Table3") {(results: [String]) -> Void in resultsCallbackHandler(results, outputArray: &self.array3)}
}

func queryTable(tableName: String, callback: (foundStrings: [String]) -> Void) {

    let query = Query(tableName: tableName)
    query.findStringsInBackground({ (results: [String]) -> Void in

        callback(results: results)
    })
}

// this will get called each time one of the global arrays have been updated with new results
func reloadMyView() {

    // do something with array1, array2, array3
}