简体   繁体   English

如何运行mstest单元测试,以便基础数据库有时间正确构建?

[英]How to run mstest unit tests so that an underlying database has time to build properly?

I've got a C# library that I've written a lot of unit tests for. 我有一个C#库,我为此编写了许多单元测试。 The library is a data access layer library and requires SQL Server's full-text indexing capabilities, which means LocalDb will not work. 该库是数据访问层库,并且需要SQL Server的全文本索引功能,这意味着LocalDb将无法工作。 The unit tests are connecting to a local SQL Server instance. 单元测试正在连接到本地SQL Server实例。 The project has an IDatabaseInitializer that drops and re-creates the database for each test, so each test has a fresh set of data to assume it can work against, meaning each test is capable of running on its own - no ordering needed. 该项目有一个IDatabaseInitializer ,它可以为每个测试删除并重新创建数据库,因此每个测试都有一组新的数据假定可以使用,这意味着每个测试都可以独立运行-无需排序。

A problem that I've had since day one on this, but never tackled yet, is that if I simply run all of the tests at once, some will fail. 从第一天起我就遇到了一个问题,但是至今尚未解决,那就是如果我仅一次运行所有测试,那么某些测试将会失败。 If I go back and run those tests individually then they succeed. 如果我返回并单独运行这些测试,则它们会成功。 It's not always the same tests that fail when I run all at once. 当我一次全部运行时,并非总是相同的测试会失败。

I've assumed that this is because they're all running so quickly and against the same database. 我认为这是因为它们都在同一个数据库中运行得如此之快。 Perhaps the database is not properly deleting and re-creating before the next test runs. 在下一次测试运行之前,数据库可能未正确删除和重新创建。 On top of just a simple database, I've also got a SQL Server full-text index that some of my tests require in order to pass. 在一个简单的数据库之上,我还获得了一些测试才能通过的SQL Server全文索引。 The full-text index populates in the background and therefore is not "ready" immediately after populating the test database's tables. 全文索引将在后台填充,因此在填充测试数据库的表后不会立即“就绪”。 The full-text index may take a second or two to build. 全文索引可能需要一两秒钟才能构建。 This causes my tests that target the full-text index to always fail, unless I step through the initializer in a debugger and pause a few seconds while the full-text index builds. 这将导致针对全文索引的测试始终失败,除非我在调试器中逐步执行了初始化程序并在构建全文索引时暂停了几秒钟。

My question is, is there any way to avoid this clashing of database rebuilds? 我的问题是,有什么办法可以避免这种数据库重建冲突? As a bonus, is there a way to "slow down" the tests that need the full-text index, so that it has time to populate? 另外,是否有一种方法可以“放慢”需要全文索引的测试,以便有时间填充?

You can use a database with fixed datas. 您可以使用具有固定数据的数据库。 For your tests you use a transaction that you begin on TestInitialize and rollback on TestCleanup 对于测试,您使用从TestInitialize开始并在TestCleanup上回滚的事务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何并行运行单元测试(MSTest)? - How to run unit tests (MSTest) in parallel? MSTest中的ClassCleanup是静态的,但是构建服务器使用nunit来运行单元测试。 我该如何调整? - ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i adjust? MSTest - 从构建服务器隐藏一些单元测试 - MSTest - Hide some unit tests from build server 在与名称模式匹配的项目中使用mstest运行所有单元测试 - Run all unit tests with mstest in projects matching a name pattern MSTest单元测试自行通过,在运行其他测试时失败 - MSTest unit test passes by itself, fails when other tests are run Azure Devops - MSTest - 如果我的单元测试失败,我如何使构建失败,但在集成测试失败时继续(带有警告/通知) - Azure Devops - MSTest - How can I fail a build if my unit tests fail, but continue (with warning/notification) when integration test fail 如何在Visual Studio 2008中配置MsTest的单元测试? - How to Profile Unit Tests of MsTest in Visual Studio 2008? MsTest的测试是否会产生实际的单元测试? - Are the tests that MsTest generates actual unit tests? VS中的Mstest Run忽略测试 - Mstest Run ignored tests in VS 在MSTest中,如何找到要在ClassInitialize或AssemblyInitialize中运行的测试总数? - In MSTest how to find the total number of tests to run in ClassInitialize or AssemblyInitialize?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM