Does NUnit have a built in way of calling tests from another unit?
I need to call a Test from a different unit to use in my current unit (by unit I mean class). Does NUnit have infrastructure to do that or should I just keep 开发者_如何学Pythondoing what I'm doing; instantiating the class and invoking the method?
Thanks!
If I understand you correctly, you have a unit test in Class1, and within that you want to run a unit test in Class2. Currently, within Class1's test, you are instantiating Class2 and calling the test method. You'd prefer the Class1 test do something like 'Nunit.RunTest(Class2.MyTest)'
As far as I know, there is no way to do this from NUnit. If you have common code you want to use in multiple locations the easiest way is to create a separate method that both tests can call. The downside is that you wouldn't be able to share Setup and Teardown methods for the two.
If you need to run Setup and Teardown for both tests, Steve Gilham's suggestion to use inheritance might be a better approach.
In general though, you should be running all unit tests when code changes. Each test should be run on it's own - do you really need to call one test from within another?
Could you elaborate as to what is the use-case here?
The most obvious automatic method is inheritance. If your current test class subclasses the other test class, then all its tests will be pulled in automatically. This is useful for testing object hierarchies, where the unit test class for the base class is itself the base class for the test classes for each derived class.
精彩评论