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
Unit tests for pydy/viz/server.py #325
Conversation
kdexd
commented
Feb 2, 2016
- This PR is in correspondence to issue server.py needs unit tests #243 and contains work on a newly added file under the directory structure as : pydy/viz/tests/test_server.py
- These tests are for the file : pydy/viz/server.py
Makes a new class `TestStoppableHTTPServer`. - Adds method test_stoppable_http_server which tests whether the StoppableHttpServer binds and stops properly. - Test is passed when the boolean `run` of StoppableHTTPServer toggles True/ False on binding/ stopping the server.
@moorepants I will keep on adding more tests incrementally. Currently, I wrote tests for the StoppableHTTPServer class in server.py.Please view the commit message and the code, and suggest me improvements as I am new to test driven development procedure. |
|
||
def test_stoppable_http_server(self): | ||
self.stoppable_http_server.server_bind() | ||
assert self.stoppable_http_server.run is True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid comparing boolean values to True or False using == or is.
https://www.python.org/dev/peps/pep-0008/#programming-recommendations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll fix this.
If you need some sample data for a scene check out the tests for the scene class. It generates some base data. |
- self.http_server declaration was not used anywhere so removed - boolean comparison using `is` refactored
|
About my previous comment, I need not generate JSON files for the purpose, I found them under |
For future reference, the output of one test should never be dependent on another. It's better to generate data at the beginning of the test or to add test data files. |
- The method `test_run_server` is broken and needs fix.
I can't find a suitable way to stop the server. Help needed here. The test here won't pass in this commit. |
You can click on the red x to look at the test failure details. You can then look at the console output for each job to see why your test is failing. The test fails before the server starts running. |
|
IPython opens the server by default. I think we were copying that functionality here. |
Shifting the line webbrowser.open(url) to a separate method does not open the server. Also I need to perform assertions and further shutdown the server by sending it SIGINT and a "Y" as stdin. But on running the tests, the server starts and nothing can be performed further. I tried to use subprocess, in the previous commit, but the method subprocess.communicate waits for the process to end. On manually stopping the server, I get an EOFError, due to killing of the subprocess and this test fails. What should I do for it? |
You call Call |
@oliverlee This trick doesn't work as the test does not move on to next line of execution once the server is started. |
@oliverlee @moorepants Hi, while testing None seem to just open the server in the background and let me programatically generate an interrupt and further provide a "y" as stdin, to shutdown the server. I thought the thread or threading modules will surely help, but the method contains a signal.signal, which is only valid in main thread. What should I do here now ? |
If you comment out the code, I cannot see how the test fails on Travis. |
The subprocess/Popen module is used to spawn new processes and is intended to replace several older modules and functions, namely We want to run the server in another thread as the current behavior blocks. I've submitted #326 to move execution into a background thread allowing you to stop it easily. Here's an example: def test_run_server(self):
self.test_server.run_server(headless=True)
assert self.test_server.httpd.running
sys.stdin = io.StringIO('y')
os.kill(os.getpid(), signal.SIGINT)
assert not self.test_server.httpd.running
assert not self.test_server._thread.is_alive() |
I don't know what directory you're running the tests in, but it fails on travis:
You need to set the directory argument of Server during initialization. |
@oliverlee That is because after one test, the directory cannot be changed to This happened on my machine too. I executed os.chdir("..") to get out of the static/ directory after doing the tests. Does travis test in the same container as appveyor ? It seems strange as I could see one test passing and another failing afterwards sometime ago. |
The directory argument is not set correctly because the working directory on travis is likely different than what you are using. Something like this will set the correct directory regardless of your working directory: os.path.join(os.path.dirname(__file__), '../static') However, I don't know if the server will modify files in place. Maybe it would be better to create a new directory for each test? @moorepants @sahilshekhawat I have no idea what appveyor does but it tests Windows whereas travis tests Linux. |
Ok, so once your PR is merged, I will pull the code and try to complete these tests. |
You can merge the PR locally and work on your tests before it gets merged into master. |
@oliverlee The tests will pass only if I press the enter key manually while I need to provide an EOF probably. |
Ok, so Travis CI builds are passing, but Appveyor are failing. Why so ? I can't see the reason, only show that the process exited with status 1. |
This is the appveyor line that sow failure:
Appveyor runs the code on windows. You have to make sure to use cross platform python commands, e.g. not os.system() or other things like that. |
It isn't actually showing the exact error, I do not have Windows distro on my laptop, only Ubuntu. Though i suspect it is |
|
@karandesai-96 Have you ensured that the changes are consistent with pep8? I remember I got the same error and got rid of it only after fixing pep8 issues. |
The minimal pep8 checks we have only run on the Travis system, not appveyor. |
Ok. Thanks. |
FYI, #326 has been merged. Maybe that will help. @oliverlee said that you can't check this on appveyor though, due to some limitation. Maybe we should only have the test that sends the kill signal run on local machines. |
Sounds like a plan 😄 |