如何在硒测试期间计算执行的查询?

时间:2017-03-29 10:56:53

标签: django django-testing

我已尝试过以下提到的代码,但它对我没用。

class SeleniumTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.driver = PhantomJS()

    @override_settings(DEBUG=True) 
    def test_queries(self)
        with self.assertNumQueries(10):
            self.driver.get(self.live_server_url + "/page-with-10-queries")

输出:

query['sql'] for query in self.captured_queries

AssertionError: 0 != 10 : 0 queries executed, 10 expected

Captured queries were:

1 个答案:

答案 0 :(得分:0)

要测试视图生成的查询,请使用以下代码:

def test_queries(self):
    with self.assertNumQueries(3):
        found = self.client.get(reverse('<url name>'))
        print(found)  # This is the line that initiates the Lazy query

以下是未经测试的建议,但如果您坚持使用硒,您可以尝试:

@override_settings(DEBUG=True) 
def test_queries(self)
    with self.assertNumQueries(10):
        found = self.driver.get(self.live_server_url + "/page-with-10-queries")
        print(found)  # Just an idea