Showing posts with label vs2012. Show all posts
Showing posts with label vs2012. Show all posts

Friday, 15 February 2013

TFS build using testing files

TFS build using testing files

Desired result

I want to run test that are using test files on server.
I am targeting VS and TFS 2012

Solution

My test solution layout.


Add all your files into folder which is part of your solution.
Set property all files you need for tests to content and copy always.
This will ensure that files are always the latest ones.




Now create test class

    [TestClass]
    [DeploymentItem(@"TestData", @"TestData")]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Assert.IsTrue(File.Exists(@"TestData\TextFile1.txt"));
        }
    }

and test result

You need to use in, out directory for Deployment Item, this will copy only existing item from your solution. I have not managed to create only empty directory.

Check if your directory has been created in location: ClassLibrary1.Tests\bin\Debug.

You can read up about DeploymentItem


Thursday, 14 February 2013

Setup nuget package feed shared directory folder

Setup nuget package in shared folder

I wanted to know how to setup local nuget packages server.

For this example I am using VS2012.

Go to the server(local pc) and create new directory. Setup the folder to be shared. You will end up with format:

\\ServerName\Nugets


Add the server to your nuget packages source.
One of the way I choose to use for opening Nuget package manager

 Now click on settings in left bottom corner.
You will get window with sources for nuget package sources. Insert name of your source (Can be anything you want), add source, your new shared folder, as network resource (See picture below). Click Add and Ok.

This is all you need to do to add new nuget package source to your VS2012.

Used resources for this article:




Thursday, 17 January 2013

VS2012 - Expose internal to test class

Lets have project for which we want to do testing of all items. We know that access public methods is easy but private or internal its bit more difficult..

Project class:




namespace ClassLibrary1
{
    public class Project
    {
        /// <summary>
        /// Everyone can access
        /// </summary>
        public void Test01()
        {
        }

        /// <summary>
        /// Cannot be easily tested without assembly
        /// </summary>
        internal void Test02()
        {
        }

        /// <summary>
        /// Do not expose if not required. Will not be accessed from test, we use instead testing accessor using Test03Testing.
        /// </summary>
        private void Test03()
        {            
        }

        /// <summary>
        /// Create accessor for testing as internal.
        /// </summary>
        internal void Test03Testing()
        {
            Test03();
        }
    }
}



Test class

Now we create new test project and we want to create test for this implementation.


using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Test01()
        {
            var project = new Project();
            project.Test01();
        }

        [TestMethod]
        public void Test02()
        {
            var project = new Project();
            project.Test02();
        }

       [TestMethod]
        public void Test03()
        {
            var project = new Project();
            project.Test03Testing();
        }
    }
}


However the compilation will end with messages and you will not get intellisense when typing the internal methods.


Now we will add "magic" line to AssemblyInfo.cs of the project that we want to test.

Default project looks like this:












Inside of the file add line:

// Expose internal methods to test class
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Project.Tests")]

This will expose all internal items to test project.

If your rebuild the project again and all is working.



See test application here:
http://dl.dropbox.com/u/84298264/CodeExamples/TestingApplication.zip

With thanks to Bryan Avery for finding out the solution :)