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

查看:24
本文介绍了我应该如何测试数据库驱动的 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.

如果您不介意稍有延迟,可以使用 谷歌网站管理员工具,它将报告其爬虫检测到的所有 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天全站免登陆