2012-05-03 3 views
2

희망 간단한 질문을 통과하지만 문서 또는 web2py 책에 포함하지 않는 것 만드는 doctest를 ... : 매개 변수와web2py : 내가 좋아하는 뭔가를 보이는 web2py 컨트롤러 방법을 가지고</p> <p>매개 변수

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

이 문서

에 따라 요청 변수로 전달되는 호출의 반환 값을 평가하기 위해 (인라인 메소드 정의와)를 doctest가를 구축 할 수있는 방법이 같은 mymethod(param1=9, param2='a', param3=3.7) ? 사전에

감사

답변

3

그냥 doctest가 내 request.vars에서 원하는 값을 넣어 :

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

가 doctest가 바로, 당신은 당신이 시작할 수있는하는 web2py 쉘에서 놀 수를 얻으려면 다음과 같이

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

(즉 -M 옵션이하는 일의) 실행 응용 프로그램의 모델 파일이있는 환경에서 당신에게 파이썬 쉘을 줄 것이다. mycontroller가 지정되었으므로 mycontroller에서 모든 함수를 호출 할 수도 있습니다. 셸에서 몇 가지 명령을 실행 한 다음 세션을 문서 문자열에 붙여 넣습니다.

+0

아하! 그래서 그렇게하는 것입니다. 나는 그것이 그 것처럼 단순 할 것이라고 기대했다. Anthony 대단히 감사합니다. – 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