-
-
Notifications
You must be signed in to change notification settings - Fork 890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use channel with the test client? #366
Comments
The I think your confusion starts from thinking that Redis is used during tests, which is incorrect. When you are testing your server, there is no real server interactions, all the sending and receiving is mocked. The So basically, your tests do not need to change at all with the introduction of a message queue. Nothing will be sent to the message queue anyway. If you need your tests to send stuff to the message queue, then the test client is not the right approach. Instead, you need to use a real server and a real client, and then use a dedicated Redis channel for your tests to take place. |
Hi Miguel, thanks for your speedy and helpful reply as always. What you said about the channels makes sense, thanks. It seems that you are right that this doesn't have anything to do with the specific configuration of the channel. However I have tried to narrow down the error I was getting with my tests, and it looks like it is to do with the redis message queue. Changing the line
back to
in my factory method makes the tests pass again. Could be this to do with how I set up my test app and client? If you're not aware of any possible gotchas, I am happy to try and construct a minimal replication of this problem when I have the chance. Thanks in advance. output of
|
Hi, I have looked into this further and will post a minimal example. However it seems from my understanding of it now that what I want it to do cannot be done. You see, I am testing functionality whereby I POST to a HTTP endpoint, and check that an appropriate message is sent out over the socket in response. To do the POST request I use the usual Flask test client. To check for the received message I use the Flask-SocketIO test client. If I understood correctly, when the Flask app is accessed with the Flask test client, it is not really possible to expect the app to emit a message any different than in production. The Flask test client does not know anything about Flask-SocketIO, and so cannot make sure that socket messages are sent over a test interface rather than to the actual Redis MQ. The Flask-SocketIO client can do this, but only in response to other socket messages, since it doesn't support HTTP requests. The Flask-SocketIO knows to listen on the test interface not the real Redis server, but the messages will not get there. Perhaps it would be possible for the Flask-SocketIO client to somehow inherit from the Flask test client and support HTTP requests, which if they lead to socket messages would go through the test interface? Otherwise it seems that I have to either a) check for messages using a fully-fledged socketio client in my tests, or b) change the factory function so that it does not configure socketio to use Redis when run in a unit test. Let me know if this reflects how you see the situation.. Thanks in advance. |
The example is at https://github.com/jwg4/Flask-SocketIO/tree/socket_testing_example Copying into the thread: (app.py)
(test_app.py)
(requirements.txt)
|
@jwg4 Your testing configuration should have |
Your first statement is right, and this is how I will try to get the tests to work. Your last statement is not right. I get an exception if the redis URL is not valid. But if it is valid, the test still fails because no message is received. |
I will check this. Configuring Redis to be used alongside the test client is a combination that does not make sense to use, so it is possible Redis interferes somehow with the mocking that the test client does. |
Thanks a lot for your help. |
I am setting up socketio in my Flask factory method like this:
The app works fine. I can also subscribe to the socketio messages outside of Flask in a Python script which does
(Being able to do this is the reason I switched to using a Redis message queue, which is also the reason I needed to use channels, as staging and several different production instances all use the same Redis server.
However, my tests using the socketio test client, which used to work before I introduced channels to socketio, now fail, because they didn't get any messages.
The setup of the tests looks like this (after some code which sets up the Flask app):
and the test does something like this
The assert fails where once it used to pass.
I don't see anywhere the channel name can be passed into the socket test client, neither when it is created nor when receiving messages, although the namespace can be passed in. Is it not possible to configure this? Or is it something that the test client should handle without passing in the channel name?
The text was updated successfully, but these errors were encountered: