Raj Aththanayake's Blog Raj Aththanayake's Blog | November 2010

Automating QUnit and JSTestDriver within VS2010

28. November 2010

I briefly touched based on this during my presentation at Code Camp. There were number of developers who were interested in browser based Unit Testing. This blog post is to provide you with some information on how you would automate browser based Unit Testing with VS2010.

QUnit is a great JavaScript Unit Testing tool which allows you write powerful browser based Unit Tests. You can find more information on QUnit here.

As described in the JSTestDriver Getting Started, you can use the below command to run both Java server and the client using a single command.

java -jar <path>/JsTestDriver.jar --port 9876 --browser firefoxpath,chromepath –tests all 

Next step is to automate the above command within VS2010. You can easily achieve this by using VS External Tools.

·         VS -> Tools -> External Tools ‘Add new’

·         Specify the Title E.g “Js Unit Tests Run”

·         Specify the Command - C:\Program Files\Java\jre6\bin\java.exe

(This is the path where your java runtime is located)

·         Specify the Arguments

-jar <path>/JsTestDriver.jar --port 9876 --browser  "C:\Program Files\Internet Explorer\iexplore.exe, C:\Program Files\Mozilla Firefox\firefox.exe" --tests all

·         Initial Directory is the path to the directory where the JSTestDriver is located.

·         Ensure you select the “Use Output Window” check box, so you can see the Unit Tests result in the output windows.

Once you run the VS external command, you should be able to see your output similar to below.

You can further automate this by running a VS macro, so every time when you save a JS file the above command will run all browser Unit Tests.

Unit Testing

Code Camp OZ2010 - Real World Unit Testing & Future

23. November 2010

I hope you all had a great time at Code Camp and a safe journey back home.

This blog post is to provide you with some additional references on the presentation Real World Unit Testing & Future. I have also included some answers to the questions which some of you have had after the presentation. At the end of this post, there is a link and you can download my presentation slides. 

Download TestLint http://site.typemock.com/test-lint/

Links to Roy Osherove’s Test Reviews.

Unity : http://weblogs.asp.net/rosherove/archive/2009/03/23/test-review-3-unity.aspx

ASP.NET MVC : http://weblogs.asp.net/rosherove/archive/2009/03/21/test-review-2-asp-net-mvc-unit-tests.aspx

Question on multiple Asserts in a Unit Test?

Post presentation questions interested me that some of you have different opinions on having multiple asserts within a Unit Test. The point I wanted to make was, as a general rule, it is best to have one assert per Unit Test.  Having said that, there may be instances where you would like to test logically related things within the same Unit Test.  Which means “Multiple aspects of the same object”. If one aspect fails then we would consider as the Unit Test is fail. 

 

        [TestMethod]
        public void TraceOutput_SimpleStringWithCommaDelimitted_AnalyseTraceData()
        {
            TraceAnaliser traceAnaliser = new TraceAnaliser();

            AppTraceOutput traceOutput = traceAnaliser.AnalyseTraceData("Europe, Africa, 10:00, Australia, Simon");

            //Different aspects of the same object being tested.
            Assert.AreEqual(traceOutput.ReadLine(1)[0], "Europe");
            Assert.AreEqual(traceOutput.ReadLine(1)[1], "Africa");
            Assert.AreEqual(traceOutput.ReadLine(1)[2], "10.00");
            Assert.IsTrue(traceOutput.LineCount == 1);
        }

 

 

If you want to make the above test more maintainable you would also consider comparing the objects. For example...

Assert.AreEqual(traceOutput, expected);

Having multiple asserts in Unit Tests raise maintainability issues. If the first assert fails, then the subsequent asserts will never get executed.  We would never know those subsequent asserts were succeeded or not.  Since it is a partial picture, sometimes it is very hard to isolate the problem why the Unit Test is failing. If the tests are separated and solid they are easier to read and easier to maintain.

Also I came across with another tool (NUnit Addin for Running one assert per test ) which I did not include in my talk, but please feel free to have a look at this.

http://rauchy.net/oapt/

 

The difference between the Code Contracts and Unit Tests.

There was a question on Code Contracts and Pex. Code Contracts allows you to perform powerful assertions within your production code. It encapsulates the intention of your design using contracts, so the compiler tools using Static Analysis, Runtime Analysis can be used to discover those intentions (E.g Pre Conditions and Post Conditions). Unit Test, verifies a piece of logic/behaviour within a method while allowing you to improve the design of the production code. One cannot replace each other as they both have different purposes.

 

Download Code Contracts and Pex

Code Contracts - http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx

Pex - http://research.microsoft.com/en-us/projects/pex/

 

Where can I get more information on Code Contracts and Pex?

Here is a video from PDC 2010 which explains some useful information using Pex and Code Contracts. http://channel9.msdn.com/Blogs/martinesmann/Code-Contracts-and-Pex-Power-Charge-Your-Assertions-and-Unit-Tests

 

Download Sinon, JSTestDriver and QUnit

a.      Sinon - http://cjohansen.no/en/javascript/javascript_test_spies_stubs_and_mocks

b.      JSTestDriver - http://code.google.com/p/js-test-driver/

C.      QUnit - http://docs.jquery.com/Qunit 

d.   QUnit to JSTestDriver adaptor - http://code.google.com/p/js-test-driver/wiki/QUnitAdapter

(Note : I will soon write a separate blog post on how to automate this within VS 2010)

 

Powerpoint slides. Real world Unit Testing & Future.pptx (971.90 kb)

 

Code Camp 2010 - Real world unit testing from David Burela on Vimeo.

Unit Testing