My new TestConfig Framework now on CodePlex!


I am pleased to announce that my new Testing  and Configuration Framework (TestingConf Utilities) just published on CodePlex.

http://testingconfutilities.codeplex.com/

This framework is a very lightweight framework that will help any developer like me that really believe and religion in TDD (Test Driven Development), BDD (Behavior Driven Development) Testing and Unit Testing because it has many methods that will help utilizing the testing, configuration and automation.

So you may use this framework during the running of your test methods, for example suppose you need to check if the insert method success or not?  You will need to verify the value in the DB, so there is a method that you just give it the DB-connection-string, table name, the column you want and it will get you the value directly so you can assert if it is as expected or not and so on.

Visual Studio 11 Beta has a fake library (Microsoft Fakes)



From long time ago I write about how we can use Mole in layer isolation, for more info see the post here.

But with release of Visual Studio 11 Beta, it came with Microsoft Fakes, which I think it based on Moles, the main benfit of  Microsoft Fakes or Moles the ability to mock or stub the sealed classes (no public constructors)

The Fakes framework helps developers create, maintain, and inject dummy implementations in their unit tests. The Fakes framework generates and maintains stub types and shim types for a system under test

Fore more information see the post on MSDN

 

To download Visual Studio and TFS 11 Beta click on the following link
http://www.microsoft.com/visualstudio/11/en-us/downloads

New Feature to generate test data with fluent API in DevMagicFake



Hello,

I received very good suggestions and feedback from Maatren Balliauw, really thanks Maarten for your valuable opinions and ideas  :-) the most important suggestion was about making a fluent interface for my framework (DevMagicFake), of course there are data generation but with file configuration only, I planned to support better way but really Maatern give me very good opinions,  And now I complete this feature and here how it will work

When we work we need test data for testing our application, we need generator for our classes so we can perform the desired business scenarios, now we can use DevMagicFake for data generation in our domain model using fluent API instead of file configuration.

Note: this feature not released yet, so if anyone need to use it, he will need to download the latest reversion of the project from CodePlex and just make a local build on his machine.

So if we write the following code

FluentCode

So this will generate data as the following:

DataGenerated

The rules can be set using the following methods:

DataGenerationMethods

Now there are 3 methods for putting rules for your data generation

  • RuleUsesClassProperty( )
  • RuleUsesDataType( )
  • RuleUsesPropertyOnly( )

Each method can use one of the 4 types of generation of the following

  • GeneratFromList()
  • GenerateFromRange( )
  • GenerateFromValue( )
  • GenerateFromRandom( )

Finally you can controller the null percentage if needed

  • NullPercentage( )

And remember the data generation is a secondary goal of DevMagicFake , the main goal is to provide a way of real repository for your business classes in memory with all the methods that you may need to stop writing any faking code and be productive and working real TDD (Test Driven Development ) and (Test Driven Design)

I hope to get more feedback for improvement and new feature that will really help.

Thanks

Creates test cases from test methods using tcm.exe



Tcm.exe is an application that can be used to creates test cases from test methods, we can also run test cases with associated automation test  instead of using the user interface so it can be run with the nightly build batch

When you start the run from the command line, use tcm.exe. You must specify both the test settings that contain the set of roles that you require to run your tests, and an environment that contains this set of roles.

So

Tcm.exe is a command-line utility that lets you perform the following tasks:

MVC 3 Dependency injection with Unity 2.0 Video



I just create a video that explain the new feature of MVC3 IDependencyResolver which make the DI(Dependency Injection) easier with mvc .net, I used the Unity 2.0 DI framework to inject Service A and Service B, in this video I show how to work with Unity in design-time and run-time for different services

I will use Unity Microsoft, with net MVC, as we know Microsft Unity one of the appliction block for dependency injection .net

Thanks for Jan Jonas for his post.

For more information you can visit the following link on MSDN

You can download the project from here (Download)

How to isolate layers and other library using Moles in Unit Testing?



Moles is one of the Mocking framework for isolation dependencies and other layer’s classes, I used to work with Moq framework and it was very good, but with Mole you can do very good things I don’t know if we can make it with Moq?  like you can isolate your code from .NET Framework code for example how you will isolate DateTime.Now to return specific value that you want?  Remember that you don’t have access definition to the .NET Framework Classes, but with Moles you can do that.

I create a solution from multiple projects so we can investigate Moles, the solution structure as the following:

 

Projects

Project

You will add reference in the Test project to the DB project

Right click on the DB ref and choose Add Mole Assembly

Add Reference

Moles

Start using it as the following

DB Class

using Common;
namespace DB
{
public class DataBase:IDataBase
{
public bool Save(IEmployee e)
{
return true;
}
}
}

Employee Class

using System;
using Common;

namespace DB
{
public class Employee:IEmployee
{
public int Id { get; set; }

public string Name { get; set; }

public DateTime BirthDate { get; set; }
}
}

The Method under the Test

using System;
using Common;
using DB;

namespace TryMole
{
public class MyClas
{
public IDataBase DataBase { get; set; }

public void SaveEmployee(IEmployee e)
{
if (e == null)
throw new ArgumentNullException();
if (e.Id <= 0)
throw new ArgumentException();
if (e.Id > 5000)
throw new ArgumentException();
DataBase.Save(e);
}
}
}

Test Method

[TestMethod()]
public void SaveEmployeeTest()
{
MyClas target = new MyClas(); // TODO: Initialize to an appropriate value
var database = new SIDataBase();
database.SaveIEmployee = (IEmployee emp) => true;
target.DataBase = database;
IEmployee e = new Employee { Id = 5 };
target.SaveEmployee(e);
}

I included also a project that isolate .NET Framework DateTime to return 1/1/2000 Y2K Bug

To download the project and Y2K project click the following link

download

How to limit the Pex to not explorer every block in your application



MS Pex one of the greatest tool I found for automation of the generation of the unit testing, but when you Pex your application you don’t want Pex exploration to run for every method called for the desired method, so you can use some attributes to define this option

PexInstrumentAssemblyAttribute    Specifies to instrument an assembly

PexInstrumentTypeAttribute           Specifies to instrument a type

PexAssemblyUnderTestAttribute      Binds a test project to a project


[assembly:PexAssemblyUnderTest(&quot;MyAssembly&quot;)]

[assembly:PexInstrumentAssembly(&quot;Lib&quot;)]

[assembly:PexInstrumentType(typeof(MyClass))]

Generate unit tests and run them with CI (Continuous integration) using Pex and MS Team Build 2010



Continuous Integration + Microsoft Pex = Continuous Exploration

By exploring our code with Microsoft Pex in our continuous integration, we will leverage the full power of automated test generation.

  • First let’s deifne what is the Pex in nutshell ?

Pex is a white box testing framework that do the following:

  1. Generate parameterize unit test if it is not exist
  2. Run Pex Exploration on  parameterize unit test
  3. Generate unit tests with inputs depend on 2 things
    1. The code of the method under the test
    2. The precondition of the test data and parameters in the  parameterize unit test  (PexAssume)
  4. Identify if the generated unit tests will success or fail when they run?
So I can integrate the Pex in the CI (Continuous Integration) as the following
Preconditions:
The pex will be used for the Domain and the DAL layer, I will not use Pex in the Controller or the Presentation layer (MVC)
First scenario :
  1. The developer using Pex in his/her day to day development tasks since he/she is working on the Domain or the DAL layer
  2. The developer start create the parameterize unit test manual and put any precondition as needed (PexAssume, ExpectException, etc.. )
  3. The developer run Pex Exploration on his/her manual parameterize unit test and see if there is any method generated will fail so he/she start adjust the parameterize unit test by adding  PexAssume, ExpectException and so on until all generated test methods become green which indicate that they will pass if they run
  4. The developer check-in his code (code under test and the manual parameterize unit test) in the source control
  5. The build start and run Pex Explortion against the manual  parameterize unit test and generate the unite tests and see if they has any unit test that fail so the build will fail
pex.exe MyApplication.Tests.dll /nor /ftf
Second scenario :
  1. The developer didn’t use Pex at all, the developer may use Pex for personal check only before check-in his code, the parameterize unit test will not checked-in  in the source control
  2. The developer start develop on the application
  3. The developer check-in his code in the source control
  4. The build start and run Pex to generate  parameterize unit test
  5. The build run Pex Explortion against the generated parameterize unit test and generate the unite tests and see if they has any unit test that fail so the build will fail
pex.exe MyApplication.dll /nor /ftf /erm:wizard

All you need to do to integrate Pex with MS Team Build 2010 just invoke the process of the CMD and run Pex from Command line and give it the appropriated parameters


What is the parameterized unit tests?



Before answer this question we have to answer the question what is classic unit test?

  • Classic unit test

Classic or closed unit test is the normal unit test that doesn’t take parameter and depend on static data inside the method itself, using traditional unit tests that do not take inputs lead that you have to write many tests to cover all possible inputs

[TestMethod]
public void MyTestMethod()
{
int x = 5;
int y = 10;
}
  • Parameterized unit test

Parameterized unit tests are test methods with parameters. A straightforward extension is to allow parameters, which serve as the test input. The result is a parameterized unit test

[TestMethod]
public void MyTestMethod(int x, int y)
{

}

So classic unit tests are methods without parameters, parameterized unit tests are methods with parameters. So you can use it with Data driven test from DB or Excel sheet, or you can use it with many inputs as needed

You can partition parameterized unit tests into four parts:

  1.  Assume: Assume preconditions over the test inputs.
  2.  Arrange: Set up the unit under test—determine which parameters are used to shape legal test inputs.
  3. Act: Exercise the unit under test by adding a method sequence, which specifies a scenario, and capturing any resulting state.
    You use parameters for argument values when calling other methods in the Act stage.
  4. Assert: Verify the behavior by adding assertions that encode the rules of a unit test.
[PexMethod]
        public void ContainsNumbers(string number, Employee employee, [PexAssumeNotNull]string value)
        {
            PexAssume.IsTrue(Employee != null);
            PexAssert.TrueForAll(number, delegate(char c){ return char.IsDigit(c);});
        }