I am trying to write a unit test for the following method:
public override void Run()
{
base.Run();
this.wpfApplication.Run();
}
Note that "this.wpfApplication" is an instance of System.Windows.Application.
I am using Microsoft's Test Explorer to run my tests (in VS Express 2013).
If I run the unit test it will hang on the .Run() line.
How can I create a unit test to run this method?
I already have a unit test to test the base class's implementation of the Run method. The base class is part of a framework which supports console and WPF apps.
As with almost all other cases where I call a .NET class method, I ended up writing an interface (e.g. IWpfRun) and used a mock implementation in my unit tests. It does mean that I need to do more in my constructors (as I need to specify which implementation to use).
Now my unit tests are concerned with my methods rather than mixing in concerns of .NET class methods such as running a WPF app, logging to a file etc.
My IWpfRun interface has 2 implementations. One is my WpfRun class that wraps the System.Windows.Application.Run method in override of my interface's Run method. The second implementation is my WpfRunMock class, that overrides the same method with an update of a property. I can assert the property value has been updated after the Run method has been called.
Related
Can't run xunit tests in parallel
Created 2 classes with a nonsense test which only sleeps and returns. Then inherited from an empty base class. The tests ran in parallel.
Changed the 2 classes to inherit from a real base class I need to use, which contains methods and some constants, and the tests don't run in parallel anymore.
The base class I'm inheriting from does not contain any testing related code (all fact annotated functions are in the child classes).
I'm using xUnit 2.4.1 with C# and .Net 4.7.2
I suspect there is something in the base class which prohibits running in parallel, but don't understand what. I have several async methods there which utilize an auto-generated REST client.
From documentation:
How does xUnit.net decide which tests can run against each other in
parallel? It uses a concept called test collections to make that
decision.
By default, each test class is a unique test collection. Tests within
the same test class will not run in parallel against each other.
Different classes/types can be executed in parallel. Even if class derived of some base class, it is still considered as different class.
But tests executes by Visual Studio, and by default tests executes on single thread. Check, did you enabled Tests in Parallel feature in Visual Studio test explorer?
Just to be sure, I were able to execute tests of different classes, which derived of one base class in parallel, by running tests without Visual Studio.
Update
In case when test class derives class which include own test methods, then inherited methods will be run as part of current class and so will not be run in parallel with methods of current class.
But derived class tests methods will be executed as part of this specific class and will be run in parallel with other classes which derives from it.
In this case you should observe those methods to be executed twice, one as part of "base" class and part of derived class.
I've a scenario where I'm adding a common code which every Test Case needs to invoke before executing the [TestMethod] (Similar to a [TestInitialize]/[AssemblyInitialize]).
However there are 3125 Tests and I'll need to add this [TestInitialize] along with some [DeploymentItem] attribute on all these Test Classes.
I could think of these ways
Manually change every Test Class to include TestInitialize (which is not efficient)
Have an Abstract Base class which will have TestInitialize and let every Test Case extend from this Base Class. But This again needs change in all 3125 Test Classes to extend them from the Base Class.
I could use [AssemblyInitialize] and use it. These still need change in around 82 assemblies. Some assemblies already have [AssemblyInitialize] so it will need a deeper look while pasting the code.
I was wondering - if there was a way in c#, where we can enforce every TestCase to invoke a certain method before running the tests without having to touch all these 3125 files?
I am using selenium web driver to a run a number of tests.
I have a base class which includes a lot of tests.
In my second class, called 'People', I have another set of tests. The People class inherits the Base class.
I initialise some of the tests in the base class, when run when I run the tests for the People class. My problem is, it also runs all the tests in the base class, whether or not I initialise them. This leaves me running 100 tests, which takes forever, and when I only really wanted to test about 50.
Is there any setting to stop selenium web driver from doing this?
As far as I understand your problem, it is normal. Your test framework will load your class 'People' and search for all tests. Since the tests defined in your base class belongs also to your class 'People' (for the framework it makes no difference), the framework will execute them as well.
You should'nt put any test in your base class nor initialise thing for your class 'People'. The base class should contains only utility/convenience methods and the startup/shutdown methods that are common to ALL your tests (for People and the rest).
In sub class 'People', you put all the tests and the startup/shutdown methods related to 'People'.
In sub class 'Toto', you put all the tests and the startup/shutdown methods related to 'Toto'.
etc.
Hope this help.
In relation to this question I asked earlier, I am stuck yet again with my unit test.
My current problem is in relation to testing the protected methods of my abstract Component class.
I've successfully implemented a mock class, named ConcreteComponent, which does its job very well of inheriting everything the abstract class has.
Thing is, I made this concrete class inside my unit test file. The only way of testing protected methods is to have a Private Accessor. However, I cannot create a Private Accessor inside the same file as where the unit test is, and thus, cannot access the protected methods.
I've tried to place the mock concrete class in a separate file, under a different namespace, and this now allows me to create a Private Accessor to which the unit test file can now use. It worked nicely, but then I figured I need this mock concrete class inside the same file where the unit test is.
So now I have two questions:
1) What are the possible workarounds for this problem?
2) Why can't I create a private accessor for a mock class which is inside the same file and namespace as the unit test class?
You can take a look at PrivateObject class to gain access to non-public API of your class in your tests. It uses reflection internally. Protected assets of a class are still api to an external client which in this case is a sub or derived class. So desire to test such api is understandable. I would not recommend polluting a class to expose public api just for the sake of testing protected api. However, since in your case the derived class is in a test project, you can actually provide public api to make testing easier and improve the performance (reflection will be slower and if you are running tests, continuous testing, as you make code changes, it may make test runs slower depending on the number of test etc.).
Protected functionality is there because you do not want to expose it to your client. But If it is protected then may be accessed through the public interface via some satisfying condition if it is not then it is a dead code so remove it.
so the golden rule is
1- Do not try to circumvent testing private / protected methods by using technology (Reflection etc), try to unit test private / protected through the public interface and BTW why your are using VS 2008 Test and why not NUnit instead
I have a number of xUnit tests across multiple classes. A lot of these classes rely on a setup method being called before each test and a clean up method being called after each test.
In each test class I have a constructor which calls the setup method and a dispose method like the following to clean up:
public override void Dispose()
{
Cleanup();
}
Each test class extends a base class, which extends a class which inherits IDisposable.
When I run the tests in an individual test class all the tests run fine and the dispose method is called correctly.
When I run all the tests in my solution using the test explorer in Visual Studio, a lot of the tests are failing because the clean up method isn't being called. When I debug the tests I'm not seeing dispose being called.
I installed the xunit.runner.visualstudio NuGet package to get the tests to run in Visual Studio 2019. I'm using xUnit 2.4.1.
Does anybody know why dispose is not being called when I run all the tests at once?
it does create some shared state which needs to be initialised before
each test and cleaned up afterwards
xUnit executes tests of different classes in parallel by default. Because you are using shared state between tests you need to execute all tests sequentially.
For executing tests sequentially, group all tests which using shared state into one "Test Collection".
From docs
When to use: when you want to create a single test context and share it among tests in several test classes, and have it cleaned up after
all the tests in the test classes have finished.
https://xunit.net/docs/shared-context