我应该如何测试数据库驱动的Django CMS 404错误? [英] How should I test a database-driven Django CMS for 404 errors?

查看:277
本文介绍了我应该如何测试数据库驱动的Django CMS 404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中设计了一个使用数据库跟踪文章对象的基本内容管理系统,然后在我的网站上显示这些文章对象。

I have designed a basic content management system in Django that uses the database to track Article objects, then displays these Article objects on my website.

每篇文章都使用模板我经常更新的标签。有时候,当我更新模板标签时,我会意外地在我的网站上打破一个或多个文章。例如,如果我更改给定文章引用的模板标记的必需参数,并忘记更新该文章中的模板标记代码,则文章将中断,从而导致404。

Each Article uses template tags that I frequently update. Occasionally, I will accidentally break one or more articles on my site when I update a template tag. For example, if I change the required arguments for a template tag referenced by a given article, and forget to update the template tag code within that article, the article will break, resulting in a 404.

我想要一个简单的方法来确保我更新到我的模板标签后,我的所有文章页面仍然工作。不幸的是,据我所知,没有一个简单的方法来测试,以确保我的所有文章都提供200个状态代码 - 除了手动梳理之外。

I would like an easy way to ensure that all of my article pages are still working after I make an update to my template tags. Unfortunately, as far as I know, there's no easy way to test to ensure that all of my articles serve 200 status codes — other than combing through them manually.

已经调查了Django测试框架,但它似乎不是我的问题的正确的解决方案。要自动测试我所有的404文章,我需要复制我的数据库中的Articles表,并将其作为测试的夹具加载,而

I've investigated the Django testing framework, but it doesn't seem to be the right solution to my problem. To automatically test all my articles for 404s, I'd need to duplicate the Articles table in my database and load it in as a fixture for my tests — and numerous people have warned me against cloning my live database.

在将更改部署到我的代码之后,是否有一种简单的方法来测试我网站上的所有文章中的404错误?

Is there an easy way to test all of the articles on my website for 404 errors after I deploy a change to my code?

推荐答案

这是一个比以前的问题更简单的问题。它有很多解决方案。

This is a much simpler problem than the one given in your previous question. It has quite a few solutions.

如果您不介意稍微延迟,您可以使用 Google网站管理员工具,该报告将报告所有404错误检测到它的抓取。

If you don't mind a slight delay, you can make use of Google Webmaster Tools which will report all 404 errors detected by it's crawler.

我以前不想在上一个问题中推荐。为什么我现在建议?那么这个问题似乎比较简单。您不需要实时数据库中的所有数据。所以创建固定装置要容易得多。

The very thing I was reluctant to recommend in the previous question. Why I am suggesting it now? Well the problem seems simpler. You don't need all the data in your live database. So it's much easier and quicker to create your fixtures.

你可能有一个只读副本,用于备份。如果您在只读副本上使用-k标志调用 ./ manage.py test ,则实际上可以做很多测试。如果您的代码不使用任何手动交易,您可以做更多的工作。

You probably have a read only replica that you are using for backup. If you invoke ./manage.py test with the -k flag on the read only replica you can actually do quite a lot of testing. You can do quite a bit more if your code does not use any manual transactions.

虽然不是完全设计用于查找404错误,但是可以使用一个网页抓取框架(如404)来抵御您的站点,以检测内部和外部404错误。

Though not exactly designed for finding 404 errors, a web scraping framework such as 404 can be used against your site to detect both internal and external 404 errors.

您可以编写一个 pytest 测试用例,以读取您网站上的所有URL,并使用python请求或任何其他http api通过不使用django单元测试,您不必担心设置测试数据库。 pytest可以直接从您的实时服务器中提取数据。

You can write a pytest test case to read all the URLs on your site and hit them using python requests or any other http api. By not using django unit tests, you are not troubled by having to setup a test database. pytest can asked to pull the data direct from your live server.

类似于抓取框架以上。这也可以与pytest一起使用,以便您不需要设置测试数据库。可以向主数据库上的测试用户帐户授予读访问权限。

Similar to scraping framework above. This too can be used with pytest so that you don't need to set up a test database. Read access can be granted to a test user account on the main database.

这篇关于我应该如何测试数据库驱动的Django CMS 404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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