设计实践:删除测试用例中删除之前创建的代码?
我已经编写了删除实体的测试用例。在测试用例中,我只需通过选择查询选择第一条记录并将其 id 传递给删除方法。我想要删除的实体可以有一些限制其删除的子实体。所以我想我应该首先在删除测试用例中创建一个实体,然后销毁它,这样我就不会面临子依赖问题。 在删除之前编写用于创建实体的代码是一个好习惯吗?它是一种在删除方法之前测试创建方法。请建议
编辑: 我正在 Rail 平台上工作,所以我有一些功能,例如使用固定装置加载数据库(当前未使用,面临一些相同的错误,请参阅此 https://stackoverflow.com/questions/5288142/rails-fixture-expects-table-name-to-be -前缀带有模块名称-如何禁用)。是的,我正在使用配置在测试用例运行后恢复数据库状态。
I have written test case for deletion of entity. In test case I simply pick first record by select query and pass its id to deletion method. Entity I want to delete can have some child entities restricting it from deletion. So I suppose I should create a entity first in my deletion test case and destroy same then so that I don't face issues of child dependency.
Is it good practice to write code for creation of entity before deletion. Its kind of testing creation method before deletion method.Please suggest
Edit:
I am working on Rail platform, so I have features like loading database with fixtures (not using currently, facing some error with same, see this https://stackoverflow.com/questions/5288142/rails-fixture-expects-table-name-to-be-prefixed-with-module-name-how-to-disable ). And yes I am using configuration to restore database state after test case run.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在单元测试中,您通常在运行测试之前执行某种设置。
许多测试框架都支持这种操作。通常,您不会通过外部查询来完成此操作;例如,您可以直接创建具有某些属性的对象,而不是执行外部公开的
create
查询。因为你首先直接创建对象,所以你没有测试你的创建查询代码(除非你内部创建对象的方式有缺陷,但如果你担心这一点,你也可以测试它),而你的删除代码是唯一被测试的东西。
In unit-testing, you usually perform some sort of set-up before you run your tests.
Many testing frameworks support this sort of operation. Normally you don't do it through external queries though; for instance, you could directly create an object with certain properties, instead of performing an externally-exposed
create
query.Because you directly create the object in the first place, you are not testing your creation query code (unless the way you internally create objects is flawed, but if you are concerned about that, you can test it too), and your deletion code is the only thing being tested.
这是错误的。您不应在单元测试期间执行查询。
我看到的测试可以是:
现有实体;
This is wrong. You should not execute queries during unit testing.
Test that I see can be:
existent Entity;
如果您的单元测试框架允许测试依赖项,即仅当测试 Y 通过时才运行测试 X 并将 Y 的返回值作为参数传递给 X,那么您就可以摆脱它。 PHP 中的情况如下:
如果 testCreate() 失败,PHPUnit 将跳过 testDelete()。如果您无法在每次测试运行之前设置标准测试数据集,那么这是一个很好的解决方法。
If your unit testing framework allows test dependencies, i.e. run test X only if test Y passes and pass Y's return value as a parameter to X, you can get away with it. Here's how that would look in PHP:
PHPUnit will skip testDelete() if testCreate() fails. This is a good work-around if you cannot setup a standard test data set before each test run.
是的,创建您正在测试其删除的实体是一个很好的做法,这样测试就不会依赖于外部状态,并且可以独立于其他测试而重复。
这不会测试创建,而是使用创建来设置测试删除。
如果您有多个测试依赖于相同的数据,则可以将创建拉出到您在每个需要该数据的测试中调用的方法。大多数测试框架还具有用于指定在每个测试之前运行的
setup
方法的机制,如果测试类中的所有测试都需要数据,则可以将创建的方法放在那里。Yes, it's good practice to create the entity whose deletion you are testing, so that the test does not depend on external state, and is repeatable independent of other tests.
This doesn't test creation, but uses creation in order to set up for testing deletion.
If you have multiple tests relying on the same data, the creation can be pulled out to a method that you call in each of your tests that needs that data. Most test frameworks also have a mechanism for specifying
setup
methods that are run before each test, and you could put the creation there if the data is needed for all tests in a test class.