简体   繁体   English

SpecFlow和NCrunch:在单线程中执行时出现多线程错误

[英]SpecFlow and NCrunch: multi-threaded error while executing in single thread

We updated our solution from SpecFlow1.9 to 2.0 and NUnit2.6.4 to 3.2.1. 我们将解决方案从SpecFlow1.9更新为2.0,将NUnit2.6.4更新为3.2.1。 After adapting some attributes and project settings, all tests run fine in NUnit. 调整了某些属性和项目设置后,所有测试都可以在NUnit中正常运行。 However, when the SpecFlow tests are executed with NCrunch, we get a SpecFlowException: 但是,当用NCrunch执行SpecFlow测试时,我们得到一个SpecFlowException:

TechTalk.SpecFlow.SpecFlowException : The ScenarioContext.Current static accessor cannot
 be used in multi-threaded execution. Try injecting the scenario context to the binding
 class. See http://go.specflow.org/doc-multithreaded for details.
 at TechTalk.SpecFlow.ScenarioContext.get_Current()

We intentionally designed our SpecFlow tests for a single-threaded environment (to keep the effort low) and we just want to continue executing these tests in one thread. 我们特意为单线程环境设计了SpecFlow测试(以减少工作量),而我们只想继续在一个线程中执行这些测试。 So instead of injecting the scenario context as the proposed solution (we use NInject instead of the SpecFlow mini-IoC) we're looking for some setting to convince SpecFlow that it is running in a single-threaded environment. 因此,我们不是在寻找方案上下文作为建议的解决方案(我们使用NInject代替SpecFlow mini-IoC),而是在寻找一些设置以使SpecFlow确信它在单线程环境中运行。

Here are the NCrunch 2.23.0.2 settings: 这是NCrunch 2.23.0.2设置:

I entered in the Assembly.cs files of all SpecFlow tests the following attribute: 我在所有SpecFlow测试的Assembly.cs文件中输入了以下属性:

[assembly: Parallelizable(ParallelScope.None)] 

Without success; 没有成功; the exception keeps showing up. 异常不断出现。

Does anybody have a clue how to force SpecFlow2.0 in NCrunch2.23.0.2 with NUnit3.2.1 so that it thinks it's executing in a single-threaded environment? 是否有人知道如何使用NUnit3.2.1强制在NCrunch2.23.0.2中使用SpecFlow2.0,使其认为它在单线程环境中执行?

Thank you for your effort! 感谢你的付出!

2016-5-31: update 2016-5-31:更新

I installed the new version 2.1 of SpecFlow (available since 2016-5-25) but it didn't solve the problem. 我安装了SpecFlow的新版本2.1(自2016-5-25起可用),但未能解决问题。

I created an example project with a minimum amount of code to generate the problem. 我用最少的代码创建了一个示例项目来生成问题。 The calculator implementation is statefull and cannot be tested in a multithreaded environment. 计算器实现是全状态的,无法在多线程环境中进行测试。

SpecFlow throws the exception due to the (dummy) static reference 'ScenarioContext.Current' in CustomContext. 由于CustomContext中的(虚拟)静态引用'ScenarioContext.Current',SpecFlow引发异常。 Yes I know you should inject it if you intend to run in a multithreaded test environment. 是的,如果您打算在多线程测试环境中运行,我知道应该注入它。 The problem is that SpecFlow THINKS it is in a multithreaded environment, but it isn't and it shouldn't. 问题在于SpecFlow认为它处于多线程环境中,但事实并非如此,也不应如此。

On investigation, this appears to be a 3-way compatibility problem between NCrunch, SpecFlow, and NUnit3. 经调查,这似乎是NCrunch,SpecFlow和NUnit3之间的三向兼容性问题。

As part of its behaviour, NCrunch will re-use test processes by calling into them multiple times (ie once for each batch of tests in the Processing Queue). 作为其行为的一部分,NCrunch将通过多次调用测试过程来重用测试过程(即,处理队列中的每批测试一次)。 Because NUnit3 kicks off a new thread for each test session, it ends up using a different thread for each call into SpecFlow. 因为NUnit3为每个测试会话启动一个新线程,所以最终每次对SpecFlow的调用都使用不同的线程。

SpecFlow identifies multi-threaded execution by tracking thread IDs, and since each session has a new thread, it incorrectly thinks the code is being run in parallel when actually it's just different threads being used synchronously. SpecFlow通过跟踪线程ID来识别多线程执行,并且由于每个会话都有一个新线程,因此它错误地认为实际上只是同步使用不同的线程而正在并行运行代码。

Setting the 'Test process memory limit' global NCrunch configuration setting to '1' will allow you to work around the problem, as this will cause NCrunch to throw away a test process after each batch, rather than re-using it. 将“测试过程内存限制”全局NCrunch配置设置设置为“ 1”将使您能够解决该问题,因为这将导致NCrunch在每批处理之后都放弃测试过程,而不是重新使用它。 Unfortunately, this will have a significant impact on performance. 不幸的是,这将对性能产生重大影响。

I've reported this problem to SpecFlow. 我已经将此问题报告给SpecFlow。 Because of it's nature, the most sensible thing would be for it to be fixed in SpecFlow itself - https://github.com/techtalk/SpecFlow/issues/638 由于其性质,最明智的是将其固定在SpecFlow本身中-https: //github.com/techtalk/SpecFlow/issues/638

You need to regenerate the code-behind- files of the Feature files after upgrade. 升级后,您需要重新生成功能文件的代码隐藏文件。

See the upgrade steps here: http://gasparnagy.com/2016/01/specflow-tips-how-to-upgrade-your-project-to-specflow-v2/ 请参阅此处的升级步骤: http : //gasparnagy.com/2016/01/specflow-tips-how-to-up-grade-your-project-to-specflow-v2/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM