Friday, January 28, 2011

QUnit tests under Continuous Integration

My team is addicted to writing unit tests and our CI is always hungry for more. So we decided to write unit tests for our javascript. There are a lot of choices of javascript testing frameworks. We chose to go with QUnit mostly because we use jQuery. Writing the QUnit tests were really easy and fun. Now we wanted to take it further and run our QUnit tests within our TeamCity Continuous Integration. To run the QUnit tests we wanted to use Selenium to open up the web pages with our tests and parse the results. I googled around to find if anyone else had already solved this problem, but the closest result I found was someone who used Watir instead.

Learning from the lost techies example, I wrote a parser using Selenium. I have a QUnit class which extends my Selenium class. The QUnit base class opens the supplied webpage with the QUnit tests using Selenium, reads all the html results, converts it to xml, traverses the xml and parses the results. This simply checks to see all your qunit test results pass. If one of them fails the entire Selenium test fails.

Here is the base class for all my QUnit tests:
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using NUnit.Framework;
using TidyNet;

namespace MyProject.UI.Tests {
 public abstract class QunitTest : SeleniumTest {

  public string WebPage;

  public override void SetUp () {

  public override void TearDown () {

  public void RunQunitTests () {

   var tests = GetQUnitTestResults();

   foreach (UnitTest test in tests) {
    Assert.IsTrue(test.pass, test.message);


  protected IEnumerable GetQUnitTestResults () {
   var html = new StringBuilder("<" + "html>"); //I can't have an html tag in my blogger post :X
   html.Append("<" + "/html>");
   var xml = convertToXML(html.ToString());
   var stream = new StringReader(xml);
   var xmlReader = new XmlTextReader(stream);
   var xpathDoc = new XPathDocument(xmlReader);
   var xpathNav = xpathDoc.CreateNavigator();
   var nodeIterator = xpathNav.Select("//ol[@id='qunit-tests']/li");
   var unitTests = new ArrayList();
   try {
    while (nodeIterator.MoveNext()) {
     var testResult = new UnitTest {
      pass = nodeIterator.Current.GetAttribute("class", "") == "pass"
     nodeIterator.Current.MoveToFollowing("span", "");
     testResult.module = nodeIterator.Current.Value.Replace(Environment.NewLine, " ");
     nodeIterator.Current.MoveToFollowing("span", "");
     testResult.testName = nodeIterator.Current.Value.Replace(Environment.NewLine, " ");

     if (!testResult.pass) {
      nodeIterator.Current.MoveToFollowing("ol", "");
      var messages = nodeIterator.Current.SelectDescendants("span", "", false);
      testResult.message = messages.Current.Value.Replace(Environment.NewLine, " ");
      testResult.expected = messages.Current.Value.Replace(Environment.NewLine, " ");
      testResult.actual = messages.Current.Value.Replace(Environment.NewLine, " ");

   } catch {


   return unitTests;

  private string convertToXML (string html) {
   var tidy = new Tidy();

   /* Set the options you want */
   tidy.Options.DocType = DocType.Omit;
   tidy.Options.DropFontTags = true;
   tidy.Options.LogicalEmphasis = true;
   tidy.Options.Xhtml = true;
   tidy.Options.XmlOut = true;
   tidy.Options.MakeClean = true;
   tidy.Options.TidyMark = false;

   /* Declare the parameters that is needed */
   var tmc = new TidyMessageCollection();
   var input = new MemoryStream();
   var output = new MemoryStream();

   byte [] byteArray = Encoding.UTF8.GetBytes(html);
   input.Write(byteArray, 0, byteArray.Length);
   input.Position = 0;
   tidy.Parse(input, output, tmc);

   string result = Encoding.UTF8.GetString(output.ToArray());
   return result;



Here is a test which utilizes my QUnit base class:

Putting our javascript unit tests into our CI process has been extremely valuable. We've caught multiple javascript bugs within seconds of a bad js check in. If anyone is doing any cool stuff with QUnit or Selenium and continuous integration I'd love to know.


  1. thanks a lot .. I spent days trying to implement a solution with rhino and envjs and I failed, I finally found a solution that works for me. Thanks.

  2. Excellent post!!! Selenium automation testing tool makes your software validation process lot simpler. Keep on updating your blog with such awesome information. Selenium Course in Chennai

  3. Latest technology have created a greater impact over testing web applications. This vital in identifying important issues that raises in web appplications. Thanks for sharing this information in here. Keep blogging article like this.

    Selenium training in chennai | Best selenium training institutes in chennai | Big Data Training Chennai

  4. The content published here was worth able to read and share. The aspect in which you have written the content is amazing. I have bookmarked this page for future use. Thanks for sharing this in here. Keep blogging content like this.

    Software testing training in chennai | Software testing course in chennai | Software testing institute in chennai

  5. The usage of third party storage system for the data storage can be avoided in cloud computing and we can store, access the data through internet.
    cloud computing training in chennai | cloud computing courses in chennai

  6. As the demand of quality web application keeps on increasing, the tool like Selenium IDE is getting very popular all over the world. Thus, taking Selenium Training in Chennai will help you to enter software testing industry.
    Selenium Training institute in Chennai | Selenium Training Chennai

  7. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    salesforce training in chennai | salesforce training institute in chennai

  8. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    oracle training in chennai | oracle training institutes in chennai


  9. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    Selenium Training in Chennai | Selenium Testing Course in Chennai

  10. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Salesforce Training in Chennai | Salesforce Training Institute in Chennai

  11. Everyone wants to get unique place in the IT industry’s for that you need to upgrade your skills, your blog helps me improvise my skill set to get good career, keep sharing your thoughts with us.
    Salesforce Course in Chennai|Salesforce Training Chennai

  12. I heve read your blog it's very interesting and informative. Keep sharing.
    erp providers in chennai | erp software solutions in chennai

  13. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp in chennai.

  14. Nice post. Very interesting to read. Thank you for Sharing.
    erp software in chennai

  15. Thank you for sharing such a informative information with us. Keep on sharing the blog like this.Dot Net Training Institute in Chennai | Dot Net Training Institute in Velachery

  16. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    Webseiten Gestaltung Lüdenscheid

  17. I found a lot of interesting information here. A really good post
    office 2011 mac download deutsch

  18. thankyou for the nice post on the topic which i am searching.

    java training in bangalore

  19. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.selenium training in bangalore


  20. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Selenium Training in Jayanagar

  21. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    white label website builder

    mobile website builder

  22. There is a desire to earn a lot? Then come to us and win. top slot machine games online .Fate favors you, come in and win.

  23. Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
    German Classes in Chennai
    German Language Classes in Chennai
    Hadoop Training in Chennai
    Python Training in Chennai
    German classes in OMR
    German classes in Porur
    German classes in Tnagar

  24. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Data science Course Training in Chennai |Best Data Science Training Institute in Chennai
    RPA Course Training in Chennai |Best RPA Training Institute in Chennai
    AWS Course Training in Chennai |Best AWS Training Institute in Chennai

  25. Oh, that's Great Post Thanks for sharing with us.
    This Big Data Hadoop Training in Pune Platform helped me to learn advanced concepts of all the trending technologies out there in the current IT market. really worth trying them.

  26. Hi, thanks for the sharing, super interesting information, i learned very well.If you are looking for any data science related information please visit our website