Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

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

, 4 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
I started using xUnit.Net few weeks ago. My first question was how to do the things I was used to do in other testing frameworks like MSTest or NUnit, specially when using this frameworks not only for unit testing but for higher level tests like Selenium RC web tests. So far, the framework seems to

I started using xUnit.Net few weeks ago. My first question was how to do the things I was used to do in other testing frameworks like MSTest or NUnit, specially when using this frameworks 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

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 the user information, execute different actions and validations and log out at the end:

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:

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();
    }
}

License

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

Share

About the Author

mariangemarcano

Denmark Denmark
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 Jan 2011
Article Copyright 2011 by mariangemarcano
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid