Visual Studio 2012 auto create unit test - WP8 application - unit-testing

So, I was also surprised that the "create unit test" is not available from the context menu when right clicking over a method, as opposed to Visual Studio 2010.
The work arounds suggested was to create the unit tests in VS2010 and then import it back to VS2012.
Then i found this post http://dl.my/2013/enable-create-unit-tests-on-visual-studio-2012/
and that indeed worked!
But now, after i click the "create unit test" over a method, the generated class only contains a TestContext property, without the test methods that was chosed in the wizard previously.
This is the generated class:
/// <summary>
///This is a test class for XXXTest and is intended
///to contain all XXXTest Unit Tests
///</summary>
[TestClass()]
public class XXXTest
{
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
}
I have searched the web with no results..
Does anybody know what's the issue here?

Apparently, it is not possible to use "Generate Unit Tests..." wizard for Windows Phone projects.
As you try to use the wizard, the following error will be prompted:
While trying to generate your tests, the following errors occurred:
You can only add WinMD references to a project targeting Windows 8.0 or higher. To learn how to retarget your project to a different version of Windows, please see the 'Core subgroup' section underneath the 'Windows tab' section in the 'How to: add or remove references by using the Reference Manager' help page: http://msdn.microsoft.com/library/hh708954(v=vs.110).aspx.
The best you can do now is by creating it manually.

Related

MSTest setup/teardown methods that run before and after ALL tests

Relatively new to MSTest v2 in Visual Studio 2019. The TestInitialize attribute indicates the method should run before each and every test. Similarly, TestCleanup indicates the method should run after each and every test.
[TestInitialize()]
public void Setup()
{
// This method will be called before each MSTest test method
}
[TestCleanup()]
public void Teardown()
{
// This method will be called after each MSTest test method has completed
}
If your test class has N methods, the above methods will run N times.
Is there a way to signal setup and teardown-like methods that run once only? In other words, for each complete run through all N tests, each method will run once only.
Are there similar mechanisms for NUnit3 and xUnit v2.4.0?
After some hunting, I stumbled on this website with a MSTest "Cheat Sheet" which has examples of what I was looking for (in MSTest):
[ClassInitialize]
public static void TestFixtureSetup(TestContext context)
{
// Called once before any MSTest test method has started (optional)
}
[ClassCleanup]
public static void TestFixtureTearDown()
{
// Called once after all MSTest test methods have completed (optional)
}
The ClassInitialize method must be public, static, return void and takes a single parameter. The ClassCleanup method must be public, static, return void and takes no parameters.
For NUnit, the attribute references can be found here:
[OneTimeSetUp]
public void TestFixtureSetup()
{
// Called once before any NUnit test method has started (optional)
}
[OneTimeTearDown]
public void TestFixtureTearDown()
{
// Called once after all NUnit test methods have completed (optional)
}
The OneTimeSetUp method must be public, but can be static or an instance method. Same for the OneTimeTearDown method.
xUnit does not appear to support the Setup / Teardown feature.

Testing Gradle Plugin That Uses project.gradle.projectsEvaluated

I am trying to test a Gradle plugin that uses the BuildAdapter#ProjectsEvaluated function to allow custom configurations and dependencies to be added to my project. The problem that I'm having is that if I evaluate the project using InternalProject#evaluate in the junit test then the projectsEvaluated function is never called. If I switch to the more robust GradleRunner then I do not appear to have the ability to inspect the project(s) later to actually see if my plugin works. I have provided sample code below that shows a sample plugin and a junit test. Does anyone know how I could test this functionality?
class MyPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
project.extensions.create("myPlugin", MyPluginExtension)
project.gradle.projectsEvaluated {
/*
custom logic that needs to evaluate prior
to gradle adding dependencies to project
*/
}
}
The JUnit is provided below:
class MyPluginPluginTest {
private Project project
#Before
void setup() {
project = ProjectBuilder.builder().build()
project.repositories.mavenCentral()
project.apply plugin: 'java'
project.apply plugin: MyPlugin
}
/*
Test to check whether or not the correct dependency
was added to the project when the plugin was evaluated
*/
#Test
void projectHasCheckerFrameworkDependencies() {
((ProjectInternal) project).evaluate()
Set<File> files = project.configurations.getByName('myPlugin').resolve()
assertNotEquals(0, files.size())
assertTrue(files.any { it.name.endsWith("myDependency-${project.jarName.version}.jar") })
}
}
In order to gain access to the information I required I added the projectsEvaluated closure to the projectExtensions as follows:
class MyPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
project.extensions.create("myPlugin", MyPluginExtension)
def projectsEvaluatedClosure = {
/*
custom logic that needs to evaluate prior
to gradle adding dependencies to project
*/
}
project.extensions.add("myPluginProjectsEvaluatedClosure", projectsEvaluatedClosure)
project.gradle.projectsEvaluated closure
}
}
Now that the projectsEvaluated closure is accessible in the project I executed the closure manually from the test case. This isn't an ideal solution, but I was able to verify that the code was working properly.

Unable to run tests with XUnit in parallel in Visual Studio

I have two classes TestClass1, TestClass2
I have set MaxParallel threads through assembly in AssemblyInfo.cs file in my test project:
[assembly: Xunit.CollectionBehaviorAttribute(MaxParallelThreads = 4)]
Reference Set maximum parallel threads
I have installed xunit-2.0.0-beta4-build2738(Prerelease). Also installed Xunit runner to find tests.
From my knowledge and research tells that Xunit does not run tests in the same collection in parallel. Reference Using test collections in xUnit.net v2 that is why for both the classes below I have used different collections.
Visual Studio 2013 finds my tests but when I run all the tests, it still runs tests serially. I want them to run in parallel.
If someone could help that would be great!
My code below:
Namespace1
namespace name1
{
public class MFixture
{
}
[CollectionDefinition("Test1")]
public class CollectionClass : ICollectionFixture<MFixture>
{
}
[Collection("Test1")]
public class TestClass1
{
// variables
public TestClass1()
{
//code
}
[Fact]
public void method1()
{
//code
}
[Fact]
public void method2()
{
//code
}
}
}
Namespace2
namespace name2
{
public class MFixture1
{
}
[CollectionDefinition("Test2")]
public class CollectionClass1 : ICollectionFixture<MFixture1>
{
}
[Collection("Test2")]
public class TestClass2
{
// variables
public TestClass2()
{
//code
}
[Fact]
public void method12()
{
//code
}
[Fact]
public void method22(){
{
//code
}
}
}
Other references
How to run Selenium tests in parallel with xUnit
XUnit.net attributes
XUnit.net issues
In xUnit, Each test class is a unique test collection. Tests within the same test class will not run in parallel against each other.
So in order to achieve that, you need to keep your tests in different collection(which you want to run in parallel), however that is not efficient way to solve your issue.
I answered this as per my knowledge and the content which I got on Internet, you can also refer to this link
It seems that Visual Studio doesn't support running tests in parallel. I haven't tested with the exact versions you asked about, but have found other comments saying as much. As suggested, running from the command line should work.
For me, I'm using VS2015, dnx 1.0.0-rc1-final, xunit 2.1.0, xunit.runner.dnx 2.1.0-rc1-build204 and can confirm that VS Test Explorer is dumb.
According to the docs:
By default, each test class is a unique test collection.
So given some contrived "tests" like these, it should be easy to see if they run in parallel or not:
Visual Studio:
Command line:

Visual Studio 2013: Creating ordered tests

Can someone suggest a way to run tests in a specific order in Visual Studio 2013 Express?
Is there a way to create a playlist for the tests, which also defines the order in which to run them?
By the way: These are functional tests, using Selenium, written as unit tests in C#/Visual Studio. Not actual unit tests. Sometimes a regression test suite is so big it takes a while to run through all the tests. In these cases, I've often seen the need to run the test in a prioritized order. Or, there can be cases where it's difficult to run some tests without some other tests having been run before. In this regard, it's a bit more complicated than straight unit tests (which is the reason why it's normally done by test professionals, while unit tests are done by developers).
I've organised the tests in classes with related test methods. Ex.: All login tests are in a class called LoginTests, etc.
Class LoginTests:
- AdminCanLogin (...)
- UserCanLogin (...)
- IncorrectLoginFails (...)
- ...
CreatePostTests
- CanCreateEmptyPost (...)
- CanCreateBasicPost (...)
...
These classes are unit test classes, in their own project. They in turn calls classes and methods in a class library that uses Selenium.
MS suggests creating an "Ordered Unit Test" project. However, this is not available in the Express edition.
To address your playlist request directly see MS article: http://msdn.microsoft.com/en-us/library/hh270865.aspx Resharper also has a nice test play list tool as well.
Here is an article on how to setup Ordered Tests but you cannot use this feature with Express as it requires Visual Studio Ultimate, Visual Studio Premium, Visual Studio Test Professional. http://msdn.microsoft.com/en-us/library/ms182631.aspx
If you need them ordered then they are more then likely integration tests. I am assuming you would like them ordered so you can either prepare data for the test or tear data back down after the test.
There are several ways to accommodate this requirement if it is the case. Using MSTest there are 4 attributes for this you can see more details of when they are executed here http://blogs.msdn.com/b/nnaderi/archive/2007/02/17/explaining-execution-order.aspx.
My other suggestion would be to have a helper class to preform the tasks(not tests) you are looking to have done in order, to be clear this class would not be a test class just a normal class with common functionality that would be called from within your tests.
If you need a test to create a product so another test can use that product and test that it can be added to a shopping cart then I would create a "SetupProduct" method that would do this for you as I am sure you would be testing various things that would require a product. This would prevent you from having test dependencies.
With that said, integration tests are good to verify end to end processes but where possible and applicable it might be easier to mock some or all dependencies such as your repositories. I use the Moq framework and find it really easy to work with.
This code is from the blog post linked above, I am placing it here in case the link ever dies.
Here is an example of a test class using the setup / tear down attributes to help with your tests.
[TestClass]
public class VSTSClass1
{
private TestContext testContextInstance;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[ClassInitialize]
public static void ClassSetup(TestContext a)
{
Console.WriteLine("Class Setup");
}
[TestInitialize]
public void TestInit()
{
Console.WriteLine("Test Init");
}
[TestMethod]
public void Test1()
{
Console.WriteLine("Test1");
}
[TestMethod]
public void Test2()
{
Console.WriteLine("Test2");
}
[TestMethod]
public void Test3()
{
Console.WriteLine("Test3");
}
[TestCleanup]
public void TestCleanUp()
{
Console.WriteLine("TestCleanUp");
}
[ClassCleanup]
public static void ClassCleanUp()
{
Console.WriteLine("ClassCleanUp");
}
}
Here is the order that the methods were fired.
Class Setup
Test Init
Test1
TestCleanUp
Test Init
Test2
TestCleanUp
Test Init
Test3
TestCleanUp
ClassCleanUp
If you give more information on what you are trying to accomplish I would be happy to assist you in when to use which attribute or when to use the help class, note the helper class is NOT a test class just a standard class that has methods you can utilize to do common tasks that may be needed for multiple tests.

How to output log in MS Unit Testing Framework VS 2010

I am trying to log some information while the unit test is running in MS Unit Testing Framework VS 2010.
I tried Trace.WriteLine, Console.WriteLine and Debug.WriteLine but I am not able to see the output in the output window.
Any idea how to do it? Thanks in advance
Make sure your test class contains the following:
private TestContext testContextInstance;
/// <summary>
/// Gets or sets the test context which provides
/// information about and functionality for the current test run.
/// </summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
Then you can call:
this.testContextInstance.WriteLine("Hello World");
The output from the test case is not visible in visual studio's output window. Rather it is visible in the "test results window". In the test result window, you should double click on the result of the test case (Passed/addTest row in the picture) for which you want to see the output and there you will see the all your writeLines.