Inheriting test methods across projects - c#

I'm trying to have a test class for the VS test framework, that is derived from another test class and inherits all its test methods (so that each appears twice). This was suggested in a comment on another ticket.
When this is done within the same project, it works fine. When done across projects, everything compiles fine but the new tests don't show up.
In this minimal solution, the problem is demonstrated. Two projects, in each we derive from the same base class in the first project. But the new (duplicate) tests only show up for the same-project child class.
I suspect something is optimized away in someway, but don't know what or how top debug this. I tried adding a dummy TestMethod to the second-project test class, just so that something must happen with the class - then only this test method shows, without the derived methods. Suggestions would be appreciated.

Related

ClassInitialize doesn't get called when I run all tests

ClassInitialize used to work perfectly fine, I have 2 sections in there 2 class initializers and 2 class cleanups. One of them are always active and the other are commented out because of local testing and production. At first everything was working ok, then I switched the ClassInitialize and ClassCleanup when I went back to testing on my local machine. (Keep in mind all of this code was working fine before separately.) Now when I run all tests it just skips the ClassInitialize and Cleanup and I do have TestContext in there. If anyone has any idea why it would just skip this class I would really appreciate it.
This did happen before and then I created a new class and just copy pasted the code and it worked after that, I don't want to do that every time I switch from production to local.
Most likely it's MsTest framework.
Make sure:
1 - the method marked with [ClassInitialize] attribute is static.
2 - the method expects a parameter of type TestContext.
If the framework doesn't react to the attribute, you can try using [ModuleInitializer] attribute.

Unit test C# console application ('using' not working)

I am working with Visual Studio 2015. I have created a new C# console application and a new Unit Test Project. I want to connect the two of them, to be able to do unit tests. I have added a reference to the console application in the Unit Test Project. But when I try to add the using statement, it does not pop up in Intelli-Sense.
With a class library instead of a console application, this worked fine. Why doesn't this work, and how do I get it to work?
Thanks in advance
You need to create new public class in console application, where you will write your functionality, after that you need to create an instance of this class in the unit test and test the functionality.
Class Programm its an entry point which parses the arguments, so you dont need to have there any logic and also you dont need to test it.
The fix is a combination of all your answers/comments.
IntelliSense does not show the Console App. I don't know why, maybe because there is no public class yet. I can type it manually. It won't work calling main or the main class (called 'Program'). But if I create a public class with a constructor, I can call it from the unit test.
So, basically, my template works, and can now be used to build my future TDD projects on.
Thanks to you all!

Win10 UWP, Prism/Unity and Unit Tests plus Class Libraries, silent failure

Basic layout is currently a simple app. Nothing fancy, I've got Views in the App.Views namespace and my ViewModels in the App.ViewModels namespace. The ViewModels are autowired to the Views through the XAML directive:
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
So, basically, this works. Then I wanted to leverage Unity's IoC / DependencyInjection for some unit tests.
The usual way would be to simply add a Windows 10 Unit Test App to the existing app and reference the latter in the Unit Test App.
This will crash upon executing the unit tests because it seems that you may not derive the App class from anything other but Application.
I.e. this works:
public sealed partial class App : Application
This does not:
public sealed partial class App : PrismUnityApplication
It's probably also not Prism's fault and something Microsoft has to fix on their end.
Now, the proposed workaround is to simply put whatever you want to unit test into a class library and reference this library from the unit test app. This works for unit testing. It also works for the Models.
My naive approach does not work, however, for the ViewModels. The ViewModel classes are still found under the App.ViewModels namespace, as before, just that they're now in a Class Library. I can access them programmatically in the main app just fine. But upon running the program, the AutoWiring silently fails without an error.
Even when I do something like this, it still does not work:
ViewModelLocationProvider.Register(typeof(MainPage).ToString(), () => new ViewModels.MainPageViewModel());
I'm not that experienced with the technologies involved yet so without an actual error I'm a bit at a loss.
edit: Just to add to the mystery - this code does work, regardless of whether the ViewModel resides in the main app or the class library:
var x = Container.Resolve(typeof(ExamplePageViewModel)) as ExamplePageViewModel;
You are correct, this is a limitation of UWP application testability in general. The testability of UWP apps is imperfect right now. In order to fix the first issue, you need to add the BindableAttribute to your application class:
[Bindable]
sealed partial class App : PrismUnityApplication
{
}
As far as pulling your Views/ViewModels out into separate assemblies, this issue is due to how UWP handles loading types form separate assemblies. None the less, this shouldn't stop you from testing your ViewModels. You can use a regular class library to test your ViewModel logic. You will mock your dependencies. You shouldn't be creating instances of your Views to test your ViewModels. So the ViewModelLocator becomes a non-issue.
We got the same issue as you, test silently failing.
when we looked at the Test output window we saw this:
App activation failed.
Failed to initialize client proxy: could not connect to test process.
Inside our [UnitTestApp], which inherits [PrismUnityApplication], we override [ConfigureContainer] method, and setup mocks on the container, instead of setting up real classes, i.e.
UnitTestApp.Current.Container.RegisterInstance(myMock.Object, new ContainerControlledLifetimeManager());
You may also have the Container setup in each test class' constructor
This way we got our test running without a silent failure.
It seemed that the silent failure was due to the container of the actual App class being called in the context of the UnitTestApp class - but I cannot confirm 100%

What governs when [BeforeScenario] is run in SpecFlow?

I've recently started on an existing project that uses SpecFlow.
I've added a method with [BeforeScenario] in BaseSteps.cs that does some logging. BaseSteps.cs doesn't have a [Binding] attribute on its class, but the derived classes do have [Binding].
However, an example.feature can use steps from differentDerivedSteps.cs classes. In these case the [BeforeScenario] is being called multiple times in a single scenario from that feature.
Why is this happening? What is calling the BeforeScenario multiple times for a single scenario?
Some code might help identify the issues, but it might be that the derived steps classes all have the method [BeforeScenario] (as they inherit it) and so specflow is calling once for each derived class.
In Specflow its usually not necessary to get involved with any inheritance as all steps are global and accessible from anywhere, so just move your [BeforeScenario]into its own class, whack a [Binding] attribute on it and Specflow will find it an use it.
Avoid using inheritance in your Steps classes - I've found it sometimes causes weird "multiple matching bindings found"
First answer here explains why inheritance causes confusion with Specflow steps:
https://stackoverflow.com/a/15508054/2213092
Without code it's difficult to determine why it's calling that specific BeforeScenario multiple times though. If you're still troubleshooting this, you can put a breakpoint on the BeforeScenario method, and look down the call stack to see where it's being fired from.

How do I find what methods are called from a C# class method - NOT at run time

I have a C# class that has far too much code in, and I want to refactor it. What I would like to do is start with all the public methods, and build a tree for each one, showing which other methods in the class are called from it, and then which are called from the child one, and so on.
This will enable me to see which private methods belong solely to one public method, which are shared and so on.
Note that I DON'T want to do this at run time, I want to be able to look at a class, either directly at the .cs file, or using reflection on the compiled DLL.
I know I can use reflection on the compiled DLL to get the methods, but I can't find any way of finding out which methods are called by other methods in the class.
Anyone any ideas? Again, this is NOT a run time issue, it's purely to build a reusable utility to help refactor an oversized class. There are quite a few in the solution I'm working on, so the code woudl be used over and over again.
Visual Studio 2010 has action "View Call Hierarchy" where you can see all places in your solution where your code is invoked.
From my experience this static analysis may be somewhat lacking, for example method could be called dynamically using Reflection, through Data Binding, through Dependency Injection Container, etc.
Also, that may be bit off topic, and not applicable in your case, but I find a good way to find dead code for component is to have a suite of integration tests. Then you can run code coverage, and visually see what paths of code are never executed.

Categories

Resources