如何加快 Visual Studio 测试资源管理器中的“运行直至失败”功能?

问题描述 投票:0回答:1

我有一次测试失败了,我想再次失败。当我手动开始测试时,我必须等到确定要运行的测试和其他活动,因此一次测试运行大约需要 3 秒。当我开始多个测试时,只有第一个测试需要很长时间,其他测试大约需要 100 毫秒才能完成。当使用“运行直到失败”功能时,我希望测试能够快速运行,但每次运行需要 2-4 秒。有没有办法配置此行为?

所以我只是尝试“运行直到失败”(Ctrl + R,U),每次运行都需要比我想要的更多时间 我不喜欢创建测试的想法,我将只调用我的测试 1000 次,因为我每次都需要清空测试数据库(我执行测试套件构造函数)

c# .net visual-studio unit-testing xunit
1个回答
-1
投票
Disable Test Discovery Between Runs: By default, Visual Studio re-discovers tests between each run, which can slow things down. To prevent this, you can disable automatic test discovery and manually trigger it when necessary. Go to Tools > Options > Test > General and uncheck "Discover tests in real-time from source files".

Optimize Test Setup and Teardown: Since resetting the test database takes time, you might consider optimizing your setup and teardown processes. If possible, reuse test data instead of resetting the entire database for each run, especially when running the same test repeatedly.

Test in Parallel: If the test suite supports it, try enabling parallel execution to allow multiple tests to run simultaneously. In .NET, you can configure parallel test execution in the .runsettings file by setting the MaxCpuCount to the number of CPU cores you want to utilize.

Use Faster Database Cleanup Methods: If your tests involve cleaning a database, using database transactions or truncating tables instead of dropping and recreating tables can speed up the process.

Warm Up the Test Run: As you've noticed, subsequent runs are faster because the environment is already set up. Consider running the test manually once to warm up the environment before using "Run until failure."

By optimizing these factors, you should be able to reduce the time it takes for each test run in the "Run until failure" loop.
© www.soinside.com 2019 - 2024. All rights reserved.