Click here to Skip to main content
13,091,548 members (68,074 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 4 Jan 2011

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

, 4 Jan 2011
Rate this:
Please Sign up or sign in to vote.
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:

using Microsoft.VisualStudio.TestTools.UnitTesting;

public class AdminPageTest
    static SeleniumWebTestContext test = new SeleniumWebTestContext();

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

    public static void ClassCleanup() 
        new AdminPage(test.Driver).Logout();

    [TestCategory("WebTest"), TestMethod]
    public void TestViewMyProfile()
        var profileWindow = new AdminPage(test.Driver).SelectMyProfile();

    [TestCategory("WebTest"), TestMethod]
    public void TestAdminSearchUser()
        var userWindow = new AdminPage(test.Driver).SelectUserManagement();
        userWindow.Search("Marcano Maria");

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:

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


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


About the Author

Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

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