단위 테스트에 사용할 적절한 사전 개체에 공백이 포함 된 인수를 구문 분석하는 데 docopt를 가져 오는 데 문제가 있습니다.단위 테스트에서 공백을 포함하는 docopt 구문 분석 인수를 만듭니다.
testargs = []
def clear_args():
testargs[:] = []
return
def add_testfiles(file1='defaultfile1.txt', file2='defaultfile2.txt'):
clear_args()
testargs.append('--debug')
testargs.append(file1)
testargs.append(file2)
return
def parse_args(optlist):
argstr = ' '.join(optlist)
return docopt(downpost.__doc__, argv=argstr)
별도로 다음 인수를 주어이 개 시험을 가지고 내가 단위 테스트를 작성하고 코드를 : 여기에
내가 현재 docopt 구문 분석에 대한 인수 목록을 구성하기 위해 사용하고 코드입니다def test_exampleunittest(self):
add_testfiles()
testargs.append('-t "This is the title I want"')
self.args = parse_args(testargs)
self.post = myPythonScript.process(self.args)
self.assertEqual(self.post['Subject'], 'This is the title I want')
경우 :
-t <title> # <title> can be any string (with spaces) inside quotation marks
"A Filename with Spaces.txt" # any filename as long as it's in quotation marks
는 예를 들어, -t 인수가, 내가 할 것, 추가하려면 내가 말한 인수로 테스트하는 스크립트를 실행합니다. 아무 문제없이 받아 들여지고 예상대로 출력됩니다. 내가 공백이 포함 된 인수를 사용하는 단위 테스트를 실행하면
그러나, 나는 다음과 같은 얻을 : 잘 작동 (동일한 인수를 포함한) 같은 DICT 개체를 필요로
DocoptExit: Usage: myPythonScript [options] <file_1> <file_2>
다른 단위 테스트를.
docopt가 평소와 같이 인수를 구문 분석하도록하려면 코드에서 어떻게 변경해야합니까?
이것은 docopt의 [Github의 문제 추적기] (https://github.com/docopt/docopt/issues/129)에 다시 게시되어 답변되었습니다. 그냥 @Halst이 여기에 그의 대답을 복사 할 때까지 기다리므로 승인 된 것으로 표시 할 수 있습니다. – Isxek