如何让Django测试用例和Selenium服务器使用相同的数据库? [英] How to have Django test case and Selenium server use same database?

查看:165
本文介绍了如何让Django测试用例和Selenium服务器使用相同的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django(v1.4,使用Postgresql)项目,我已经编写了一堆工作单元测试。这些使用 FactoryBoy 生成大部分数据。



我是现在开始使用带有Selenium的 LiveServerTestCase 编写一些集成测试。我刚刚意识到我的测试和实时测试服务器使用不同的数据库。这意味着我的测试中由工厂创建的数据不可用于Selenium。



我不知道进步的最佳方式。我认为我可以使用灯具来提供可以工作的数据,虽然这是一个很痛苦的事情,但是在这个过程中却很难使用工厂。



是否有我可以继续使用工厂来生成可用于我的Selenium测试的数据的方式吗?真的我想要我的测试和LiveServerTestCase使用相同的数据库。

解决方案

您是否尝试使用sqlite作为数据库后端测试?


当使用内存中的SQLite数据库运行测试时,两个
数据库连接将由两个线程并行:运行实时服务器的
线程和运行
测试用例的线程。



$ b来自 Django文档的$ b



如果您不使用任何超出常规ORM的内容,您也可以从测试加速中获益。


I have a Django (v1.4, using Postgresql) project which I've written a bunch of working unittests for. These use FactoryBoy to generate most of their data.

I'm now starting to write some integration tests using LiveServerTestCase with Selenium. I've just realised that my tests and the live test server use different databases. Which means that data created by factories in my tests aren't available to Selenium.

I'm not sure of the best way to progress. I think I could use fixtures to supply data that would work, although this is a pain having got this far using factories instead.

Is there a way I can continue to use factories to generate data that will work for my Selenium tests? Really I'd like my tests and LiveServerTestCase to use the same database.

解决方案

Have you tried using sqlite as your database backend for tests?

When using an in-memory SQLite database to run the tests, the same database connection will be shared by two threads in parallel: the thread in which the live server is run and the thread in which the test case is run.

from Django docs

If you're not using anything beyond regular ORM, you might benefit from test speedups as well.

这篇关于如何让Django测试用例和Selenium服务器使用相同的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆