Click here to Skip to main content
15,895,509 members
Articles / All Topics

xUnit.Net – Running the Tests (ClassInitialize – ClassCleanup)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Jan 2011CPOL 17.1K   2  
xUnit.Net – Running the tests (ClassInitialize – ClassCleanup)

I started using xUnit.Net a few weeks ago. My first question was how to do the things I was used to doing in other testing frameworks like MSTest or NUnit, especially when using this framework not only for unit testing but for higher level tests like Selenium RC web tests. So far, the framework seems to be very good and extensible.

I am going to show some scenarios I have run into converting MSTest to xUnit.Net, having the following class:

C#
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class AdminPageTest
{
    static SeleniumWebTestContext test = new SeleniumWebTestContext();

    [ClassInitialize()]
    public static void ClassInitialize() 
    {
        new LoginPage(test).LoginUser("maria", "******");
    }

    [ClassCleanup()]
    public static void ClassCleanup() 
    {
        new AdminPage(test.Driver).Logout();
        test.StopWebTestContext();
    }

    [TestCategory("WebTest"), TestMethod]
    public void TestViewMyProfile()
    {
        var profileWindow = new AdminPage(test.Driver).SelectMyProfile();
        Assert.IsTrue(profileWindow.CheckFirstName("Maria"));
        profileWindow.Close();
    }

    [TestCategory("WebTest"), TestMethod]
    public void TestAdminSearchUser()
    {
        var userWindow = new AdminPage(test.Driver).SelectUserManagement();
        userWindow.Search("Marcano Maria");
        Assert.IsTrue(adminTab.VerifyEmail("my-email@domain.com"));
    }
}

Note that SeleniumWebTestContext is holding information about Selenium RC and starts the server.

ClassInitialize – ClassCleanup / IUseFixture<T>

Sometimes, we require sharing information among the methods defined in a class, for example in web tests, we want to share the logged in user information, execute different actions and validations and log out at the end:

C#
using Xunit;

public class AdminPageTest : IUseFixture<LoggedUserContext>
{
    SeleniumWebTestContext test;
    public AdminPageTest()
    {  
    }

    public void SetFixture(LoggedUserContext usrContext)
    {
        test = usrContext.Test;
    }
    ...
}

and the LoggedUserContext class will look like this:

C#
public class LoggedUserContext : IDisposable
{    
    public SeleniumWebTestContext Test;    
    public LoggedUserContext()    
    {        
        Test = new SeleniumWebTestContext();        
        new LoginPage(Test).LoginUser("maria", "******");    
    }    

    public void Dispose()    
    {       
        new AdminPage(Test.Driver).Logout();
        Test.StopWebTestContext();
    }
}
This article was originally posted at http://mariangemarcano.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --