简体   繁体   English

我可以在C#中使用反射在运行时创建单元测试吗?

[英]Can I create a unit test at runtime in C# using reflection?

I have a scenario where I'm needing to create tests at runtime based on a script. 我有一种情况,需要在运行时基于脚本创建测试。

I have a set of scenarios defined in a text file that need to be interpreted and each one should be run as an unit test under nunit. 我在文本文件中定义了一组需要解释的场景,每个场景都应作为单元测试在nunit下运行。

I've created a general IntegrationTestRunner to read the data and run one big test for each scenario that is defined. 我创建了一个通用的IntegrationTestRunner来读取数据并针对定义的每种情况运行一个大型测试。 This works fine. 这很好。

However, it seems to make good sense that each scenario should be it's own unit test. 但是,每个场景应该都是自己的单元测试似乎很有意义。

Is there a way to dynamically create nunit-based unit tests at runtime using reflection? 有没有一种方法可以在运行时使用反射来动态创建基于nunit的单元测试?

Thank you! 谢谢!

It'd be quicker and far easier to create a class that reads all the test cases from your file and creates TestCaseData objects (see this page ). 创建一个从文件中读取所有测试用例并创建TestCaseData对象的类会更快,更容易得多(请参阅此页面 )。 You can set the name of each test case via the Name property on the TestCaseData . 您可以通过TestCaseDataName属性设置每个测试用例的Name

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

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