在验收测试中检查数据库的状态是一种好习惯吗?

Is it a good practice to check the state of a database in acceptance tests?

假设有人应该为电子商务系统编写自动化验收测试。例如,您要检查当客户完成结帐操作时,他在系统中注册了一个新订单并链接到他的帐户。现在,当然,您可以检查的一件事是,有一些 UI 消息显示为 'Order completed succesfully'。但是,这并不能保证该订单实际上已保存在数据库中。我的问题是是否可以通过查询数据库来额外验证订单确实已保存。或者我应该在其他验收测试中不明确地验证这一点,例如通过在执行结帐操作之前检查订单列表(成功完成结帐操作后不应为空)?

you can check is that there are some UI messages displayed like 'Order completed succesfully'. However that does not guarantee that the order is in fact persisted in the db.

实际上这取决于。如果我们在谈论 Selenium - 他们确实建议这样的数据库验证:

Another common type of testing is to compare data in the UI against the data actually stored in the AUT’s database. Since you can also do database queries from a programming language, assuming you have database support functions, you can use them to retrieve data and then use the data to verify what’s displayed by the AUT is correct.

但是,只有当验收标准足够明确后,才应测试验收标准。如果没有此类特定的 E2E 要求 - 您不应在这些测试中包含此数据库检查。您可以将它们放在您的功能和集成级别,SUT 架构允许这样的 black-box approach. If we concider the typical N-tier(UI-Backend-DB),您的黑盒将是中间件 - 来自 UI 的输入, [skip all between], 输出在DB.

这当然会引入更多的复杂性,并且您的测试会变得脆弱(对于 UI 测试尤其如此)。此外,您应该考虑昂贵的对象并正确地keeping/disposing它们(例如,每个套件的数据库连接运行)。

恕我直言,您应该在自动测试中涵盖所有这些内容:

it is OK to additionally verify with querying the DB that order indeed was saved. Or should I verify that inexplicitly in other acceptance test e.g. by checking list of orders (which should not be empty after completing checkout operation succesfully) beforehand performing checkout operation

唯一的问题是放在哪里。