Django with splinter and phantomjs is painfully sl

2019-07-07 10:36发布

问题:

Today I tried combining django's LiveServerTestCase with splinter and phantomjs webdriver.

Here's what I do (simplified version):

class Test(LiveServerTestCase):

    def setUp(self):
        self.browser = Browser('phantomjs')

    def tearDown(self):
        self.browser.quit()

    def test(self):
        self.browser.visit(self.live_server_url)
        self.assertIn("Hello world!", self.browser.title)

Sometimes tests run fine - even though taking a second per test method to execute. But sometimes it can randomly take ~100 seconds for that single test method to execute, or it just freezes until I am out of patience to wait for it to finish.

I use django_nose as a test runner, and I pass --liveserver=localhost:8081-8181 range of ports to ./manage.py test command.

Is there any way to speed it up? Is there other web test runner I can which is faster?

Default web driver seem to be more reliable speed-wise (1-3 seconds per test method), but it's still pretty slow. I also would prefer a headless browser for testing.

回答1:

What makes the tests slow is open and close the browser on each test. A way to improve the tests time is open the browser once. You can do it using setUpClass and tearDownClass.