2

I want to start a server with the manage.py runserver command and I'm trying for some time now but don't really find the right way.

from subprocess import Popen, PIPE
from django.test import TestCase
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class ExampleClass(TestCase):

  def startServer(self):
    process = Popen(['cd C:\mypath'], stdin=PIPE, stdout=PIPE)
    process.stdin.write(['python', 'manage.py runserver'])

.
.
.
  def test_examplename(self):
    self.browser.get('http://localhost:8000')

Everytime I start the test it seems like the process is starting in background but as soon as the browser window pops up it shows me a "can't connect" error. So the server is not running.

Please note that the test is working fine when I start the server myself.

2 Answers 2

1

This thing

process = Popen(['cd C:\mypath'], stdin=PIPE, stdout=PIPE)
process.stdin.write(['python', 'manage.py runserver'])

will simply wont work since you are running a shell command in Popen. When you do a Popen it tries to run the program specified with the arguments given. You try to run cd but after it's done (and it probably failed since you are missing shell=True in the call to Popen) the process is closed, you cant write to its stdin, Popen doesnt open a shell (or a cmd instance in windows) it simply runs a program

if you want to run your server do it directly:

process = Popen(['python, 'C:\mypath\manage.py', 'runserver'], stdin=PIPE, stdout=PIPE)
Sign up to request clarification or add additional context in comments.

2 Comments

Now I understand Popen better, thanks. But it doesn't work even with your solution. Is the program Popen calls kept alive for the entire runtime of the test?
yes the process should stay up, unless it is closes itself (for example if you have a syntax error, then python will output the error and than exit). you can do process.poll() after you open it, it it returns None than the process is up, if it returns a number, it is the returncode of that Process ( a number that is not 0 means there was an error)
0

I recommend you use LiveServerTestCase instead of TestCase for tests that use Selenium. Django will take care of starting and stopping the server, so you don't have to worry about it.

For more information and an example of a test case that uses Selenium, see the docs.

2 Comments

Much simpler and much easier to work with, thanks for that. As I'm quite new to StackOverflow I'm not sure wether I should mark your question as right or not since it is not a solution to the question but a much better workaround.
The tick box says to accept an answer 'if it solved your problem'. It's fine to accept an answer if it solved your problem in a different way than you first expected. Having said that, it's completely up to you which answer you accept, if you want to accept an answer that's closer to your initial question I won't be offended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.