2012-05-03 17 views
2

希望一個簡單的問題,但它似乎並沒有在文檔或web2py的書被覆蓋......的web2py:創建文檔測試的是傳遞參數

我有,看起來像一個web2py的控制器的方法:

def mymethod(): 
    ''' 
    doctests go here 
    ''' 
    param1 = request.vars['param1'] 
    param2 = request.vars['param2'] 
    param3 = request.vars['param3'] 
    # Stuff happens... 
    return dict(result=result) 

與參數被傳遞作爲請求變量按照文檔

是否有任何評價一個調用的返回值的方式來建立一個文檔測試(內嵌方法定義),如mymethod(param1=9, param2='a', param3=3.7)

在此先感謝

回答

3

只需將所需值request.vars的文檔測試中:

def mymethod(): 
    ''' 
    >>> request.vars.update(param1=9, param2='a', param3=3.7) 
    >>> mymethod() 
    [expected output of mymethod goes here] 
    ''' 

要獲取文檔測試的權利,你可以在web2py中殼玩,你就可以開始如下:

python web2py.py -S myapp/mycontroller -M -N 

這會給你一個Python外殼與執行應用程序的模型文件的環境(這是-M選項做什麼)。由於指定了mycontroller,您還可以在mycontroller中調用任何函數。在shell中運行一些命令,然後將會話粘貼到文檔字符串中。

+0

啊哈!所以你就是這麼做的。我希望這會很簡單。非常感謝安東尼 – monch1962

0

除了@Anthony提供的優秀示例,我還嘗試使用urllib2.urlopen(...)測試一個寧靜的服務。從文檔的角度來看,代碼並不是很乾淨,但它起了作用。

@request.restful() 
def api(): 
    '''The following code demostrates how to interact with this api via python. 

    >>> import urllib2, urllib, httplib, json 
    >>> host = 'localhost:8000' 
    >>> func = 'api' # Otherwise request.function is NOT current function name during doctest 
    >>> base = 'http://%s/%s/%s/%s' % (host, request.application, request.controller, func) 


    Read all stuff. 
    >>> json.load(urllib2.urlopen(base)) 
    {u'content': []} 

    Create an entries. 
    >>> p = {'name': 'Peter Pan', 'address': 'Neverland',} 
    >>> r = json.load(urllib2.urlopen(base, urllib.urlencode(p))) 
    >>> r['id'] > 0 and r['errors'] == {} # typically as {'errors': {}, 'id': 1} 
    True 

    blah blah 

    ''' 
    # the function body goes here 
相關問題