tornado_json unit testing async methods
I'm trying to set up a unit test for a tornado_json web app.
I'm trying to test a post handler, but I'm failing miserably as the fetch
method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
Any help would be greatly appreciated.
@schema.validate(
input_schema={
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
},
output_schema={
"type": "array",
"items": {
"properties": {"type": "string"}
}
}
)
@coroutine
def post(self):
attributes = dict(self.body)
path = attributes["path"]
response = ["test"]
return response
@gen_test
def test_POST_method(self):
body = json.dumps({'path': 'bin'})
self.http_client.fetch(self.get_url('/api/listmyfiles'),
method="POST",
body=body
)
response = self.wait()
print(response.result()))
The error I get is:
asyncio.base_futures.InvalidStateError: Result is not set.
python-3.x tornado pytest pytest-asyncio
add a comment |
I'm trying to set up a unit test for a tornado_json web app.
I'm trying to test a post handler, but I'm failing miserably as the fetch
method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
Any help would be greatly appreciated.
@schema.validate(
input_schema={
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
},
output_schema={
"type": "array",
"items": {
"properties": {"type": "string"}
}
}
)
@coroutine
def post(self):
attributes = dict(self.body)
path = attributes["path"]
response = ["test"]
return response
@gen_test
def test_POST_method(self):
body = json.dumps({'path': 'bin'})
self.http_client.fetch(self.get_url('/api/listmyfiles'),
method="POST",
body=body
)
response = self.wait()
print(response.result()))
The error I get is:
asyncio.base_futures.InvalidStateError: Result is not set.
python-3.x tornado pytest pytest-asyncio
add a comment |
I'm trying to set up a unit test for a tornado_json web app.
I'm trying to test a post handler, but I'm failing miserably as the fetch
method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
Any help would be greatly appreciated.
@schema.validate(
input_schema={
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
},
output_schema={
"type": "array",
"items": {
"properties": {"type": "string"}
}
}
)
@coroutine
def post(self):
attributes = dict(self.body)
path = attributes["path"]
response = ["test"]
return response
@gen_test
def test_POST_method(self):
body = json.dumps({'path': 'bin'})
self.http_client.fetch(self.get_url('/api/listmyfiles'),
method="POST",
body=body
)
response = self.wait()
print(response.result()))
The error I get is:
asyncio.base_futures.InvalidStateError: Result is not set.
python-3.x tornado pytest pytest-asyncio
I'm trying to set up a unit test for a tornado_json web app.
I'm trying to test a post handler, but I'm failing miserably as the fetch
method seems to return an _asyncio.Future object, which never seems to complete/have a result set. I've tried to post a summary of the code, at the moment I'm just returning ['test'] item.
I've looked at https://github.com/tornadoweb/tornado/issues/1154, as well as the tornado documentation. It sounds like I need to self.stop, or self.wait() to complete the task, but I haven't worked out how to get this to work, or if that is the solution.
Any help would be greatly appreciated.
@schema.validate(
input_schema={
"type": "object",
"properties": {
"path": {"type": "string"}
},
"required": ["path"]
},
output_schema={
"type": "array",
"items": {
"properties": {"type": "string"}
}
}
)
@coroutine
def post(self):
attributes = dict(self.body)
path = attributes["path"]
response = ["test"]
return response
@gen_test
def test_POST_method(self):
body = json.dumps({'path': 'bin'})
self.http_client.fetch(self.get_url('/api/listmyfiles'),
method="POST",
body=body
)
response = self.wait()
print(response.result()))
The error I get is:
asyncio.base_futures.InvalidStateError: Result is not set.
python-3.x tornado pytest pytest-asyncio
python-3.x tornado pytest pytest-asyncio
edited Nov 14 '18 at 12:02
Conor
asked Nov 14 '18 at 11:52
ConorConor
1199
1199
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
AsyncHTTPTestCase
has a few different modes of operation that can't be mixed.
@gen_test
: Used withawait self.http_client.fetch(self.get_url(...))
:
@gen_test
async def test_post_method(self):
response = await self.http_client.fetch(self.get_url(...))
self.stop
/self.wait
is an older interface that is mostly (but not completely) deprecated.AsyncHTTPClient
will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.
self.fetch
is a shorthand method that combines the calls tohttp_client.fetch
andself.get_url
, and usesstop
/wait
under the covers (so it is not compatible with@gen_test
):
def test_post_method(self):
response = self.fetch('/api/listmyfiles')
If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch
. If you need to do anything else asynchronous, use gen_test
and avoid the stop/wait/self.fetch methods.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299630%2ftornado-json-unit-testing-async-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
AsyncHTTPTestCase
has a few different modes of operation that can't be mixed.
@gen_test
: Used withawait self.http_client.fetch(self.get_url(...))
:
@gen_test
async def test_post_method(self):
response = await self.http_client.fetch(self.get_url(...))
self.stop
/self.wait
is an older interface that is mostly (but not completely) deprecated.AsyncHTTPClient
will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.
self.fetch
is a shorthand method that combines the calls tohttp_client.fetch
andself.get_url
, and usesstop
/wait
under the covers (so it is not compatible with@gen_test
):
def test_post_method(self):
response = self.fetch('/api/listmyfiles')
If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch
. If you need to do anything else asynchronous, use gen_test
and avoid the stop/wait/self.fetch methods.
add a comment |
AsyncHTTPTestCase
has a few different modes of operation that can't be mixed.
@gen_test
: Used withawait self.http_client.fetch(self.get_url(...))
:
@gen_test
async def test_post_method(self):
response = await self.http_client.fetch(self.get_url(...))
self.stop
/self.wait
is an older interface that is mostly (but not completely) deprecated.AsyncHTTPClient
will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.
self.fetch
is a shorthand method that combines the calls tohttp_client.fetch
andself.get_url
, and usesstop
/wait
under the covers (so it is not compatible with@gen_test
):
def test_post_method(self):
response = self.fetch('/api/listmyfiles')
If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch
. If you need to do anything else asynchronous, use gen_test
and avoid the stop/wait/self.fetch methods.
add a comment |
AsyncHTTPTestCase
has a few different modes of operation that can't be mixed.
@gen_test
: Used withawait self.http_client.fetch(self.get_url(...))
:
@gen_test
async def test_post_method(self):
response = await self.http_client.fetch(self.get_url(...))
self.stop
/self.wait
is an older interface that is mostly (but not completely) deprecated.AsyncHTTPClient
will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.
self.fetch
is a shorthand method that combines the calls tohttp_client.fetch
andself.get_url
, and usesstop
/wait
under the covers (so it is not compatible with@gen_test
):
def test_post_method(self):
response = self.fetch('/api/listmyfiles')
If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch
. If you need to do anything else asynchronous, use gen_test
and avoid the stop/wait/self.fetch methods.
AsyncHTTPTestCase
has a few different modes of operation that can't be mixed.
@gen_test
: Used withawait self.http_client.fetch(self.get_url(...))
:
@gen_test
async def test_post_method(self):
response = await self.http_client.fetch(self.get_url(...))
self.stop
/self.wait
is an older interface that is mostly (but not completely) deprecated.AsyncHTTPClient
will not be (easily) compatible with this interface in Tornado 6.0, so I won't show an example here.
self.fetch
is a shorthand method that combines the calls tohttp_client.fetch
andself.get_url
, and usesstop
/wait
under the covers (so it is not compatible with@gen_test
):
def test_post_method(self):
response = self.fetch('/api/listmyfiles')
If the only asynchronous thing you're doing is HTTP fetches, you can use self.fetch
. If you need to do anything else asynchronous, use gen_test
and avoid the stop/wait/self.fetch methods.
answered Nov 14 '18 at 14:17
Ben DarnellBen Darnell
17.6k22036
17.6k22036
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299630%2ftornado-json-unit-testing-async-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown