带有selenium的Django测试未加载固定装置 [英] Django tests with selenium not loading fixtures

查看:45
本文介绍了带有selenium的Django测试未加载固定装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium为Django网站设置功能测试.我有一个固定装置文件( users/fixtures/users.json ),并且正在另一个应用程序( accounts )的功能测试中使用它.在运行测试时,我还运行我的开发服务器以接受来自Selenium浏览器自动化的请求.我在同一设置模块上运行 ./manage.py测试 ./manage.py runserver ,以便它们都具有同一数据库的凭据.

I am setting up functional testing using Selenium for a Django website. I have a fixture file (users/fixtures/users.json) and am using it in the functional tests in another app (accounts). When running the tests, I also run my development server to accept requests from Selenium browser automation; I run ./manage.py test and ./manage.py runserver on the same settings module so that both have the credentials for the same database.

我的帐户测试无法加载 users 固定装置:

My accounts tests are failing to load the users fixture:

from django.test import TestCase


class AccountCreationTestCase(TestCase):
    fixtures = ['users']

    # Tests depending on user login follow.
    # These tests are run via Selenium for browser automation.

当我通过命令行将灯具手动加载到测试数据库中时,这些测试成功,但是否则失败,因此,我知道当数据库中存在灯具数据时,它们正在使用灯具数据,而且我也知道他们无法加载测试夹具.

These tests succeed when I manually load the fixtures into the test database via the command line, but they fail otherwise, so I know they are making use of the fixture data when it is present in the database, and I also know that they are failing to load the test fixtures.

回顾一下:我在定义测试数据库访问凭据的同一设置模块上运行 ./manage.py test ./manage.py runserver .如果将固定装置加载到测试数据库中,那么当它们通过Selenium运行时,该数据应该可供测试使用.

To recap: I am running ./manage.py test and ./manage.py runserver on the same settings module that defines the test database access credentials. If the fixtures are being loaded into the test database, that data should be available to tests when they run via Selenium.

我想念什么?

对于上下文,我使用的是Django 1.8.另外,我正在使用Selenium来自动化PhantomJS,以便测试可以更快地运行.

Edit 1: For context, I am using Django 1.8. Also, I'm using Selenium to automate PhantomJS so that the tests can run faster.

我只是对Django文档进行了更彻底的阅读,发现了我遇到的两个问题.

Edit 2: I just did a more thorough reading of Django docs and found two issues that I'm running into.

第一个问题:Django使用自动为数据库名称添加前缀 test _ ,因此您必须确保测试服务器和Django测试按名称使用相同的数据库,如下所示:

First issue: Django tests automatically prefix the database name with test_, so you have to make sure that your test server and your Django tests are using the same database by name, like so:

DATABASES = {
    'NAME': "test_db_name",
    'TEST': {
        'NAME': "test_db_name"
    }
}

第二个问题: Django文档解释Django在测试运行之间销毁了测试数据库.这将导致开发服务器发生故障,因为它需要数据库.Django 1.8为测试运行程序引入了-keepdb 选项,该选项将在两次测试之间保留数据库实例.尽管此标志确实允许服务器在测试数据库上运行,但我似乎仍然无法将固定装置加载到该数据库中.这可能是由于使用了新标志引起的,该标志确实在测试如何加载测试数据库方面改变了测试的行为.

Second issue: Django docs explain that Django destroys test databases between test runs. This will cause the development server to fail because it requires the presence of a database. Django 1.8 introduced the --keepdb option for the test runner that will keep the database instance around between tests. While this flag does allow the server to run on the test database, I still cannot seem to get the fixtures to load into that database. This could be caused by the use of the new flag, which does significantly modify the behavior of tests in terms of how they load the test database.

推荐答案

使用Selenium进行Django测试可能很容易.概括地说,要使这样的测试正常工作,您必须处理三个主要问题:

Django testing with Selenium can be touchy. To recap, there are three main issues you have to deal with to get tests like this to work:

1)Selenium需要运行的Django服务器才能将请求退回.

1) Selenium needs a running Django server to bounce requests off of.

2)您正在运行的Django服务器应连接到测试数据库.

2) Your running Django server should be connected to the test database.

3)TestCase测试在事务中运行.由于正在运行的Django服务器无法查看测试用例的事务,因此Selenium完全看不见/无法访问您加载的所有固定装置(来源:

3) TestCase tests run in a transaction. Since the running Django server can't see into the test case's transaction, any fixtures you load will be completely invisible/inaccessible to Selenium (source: Lara's link, qris's answer).

解决方案:要解决前两个问题,您需要一个特定于测试的设置文件.在其中,您需要定义一个指向测试数据库的默认数据库,并且需要提供测试数据库的设置,以使名称匹配(请参阅我的问题中的示例).计划运行测试时,需要运行Django开发服务器,并使用测试设置文件运行测试.

Solution: To solve the first two problems, you need a testing-specific settings file. In it, you need to define a default database that points to your testing database, and you need to give your testing database's settings so that the names match (See the example in my question). When you plan to run your tests, you need to run a Django development server and your tests using the testing settings file.

要解决第三个问题,请使用 TransactionTestCase 而不是 TestCase . TransactionTestCase 旨在让开发人员在测试过程中更好地控制事务行为,因此它不会自动运行事务中的所有内容(从而使正在运行的服务器实例可以访问治具).即使这不是我们在这种情况下要尝试的操作,结果仍然是测试可以正常工作并自动清除.

To solve the third problem, use TransactionTestCase instead of TestCase. TransactionTestCase is meant to give developers finer control over transaction behavior during testing, so it doesn't automatically run everything inside of a transaction (which, in turn, gives the running server instance access to fixtures). Even though this isn't exactly what we're trying to do in this case, the result is that the tests work and clean up after themselves automatically.

注意:您可能应该考虑将黑盒测试和白盒测试彼此分开.这使您的Django测试保持原始状态",因此未来的开发人员不必花太多时间弄清楚为什么Django文档似乎并不能在所有情况下都适用.它还使功能测试与单元测试脱钩,这在团队中的开发人员无法在其计算机上访问Selenium(或Selenium的兼容版本)或仅运行单元测试的情况下非常有用.或功能测试,但不能两者兼而有之.您需要手动设置测试环境以适合您的需求,但是我相信从长远来看,这将使测试代码更整洁.

Note: You should probably consider separating black-box and white-box tests from each other. This keeps your Django tests "vanilla" so future developers don't have to waste as much time figuring out why Django documentation doesn't seem to apply in all cases. It also decouples functional tests from the unit tests, which is good in cases where a developer on your team doesn't have access to Selenium (or a compatible version of Selenium) on their machine, or when you want to run only the unit tests or functional tests, but not both. You would need to manually set up your testing environment to suit your needs, but I believe this would keep the testing code cleaner in the long run.

感谢Lara在此方面为我指明了正确的方向.

Thanks to Lara for pointing me in the right direction on this.

这篇关于带有selenium的Django测试未加载固定装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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