Archive for the 'Tests' Category
Unit Test/Functional Test/Acceptance Test
Posted by Sebastien Lachance on May 14, 2008
Listening to the last screencast of Rob Conery (MVC Storefront) made me realize something that I haven’t really understood before.
Suppose you have the interface of a repository, let’s say IEntryRepository. Then you have the actual SqlEntryRepository and TestEntryRepository. When I am doing TDD, I am testing the TestEntryRepository. But it will not be used in production. Why bothering with testing it then?
Because TDD is a design process. Even if I am testing my code, I am also designing it. The SqlEntryRepository will benefits from the actual design of the repository we use for testing.
Posted in ASP.NET MVC, Agile, Design, Learning, Tests | 2 Comments »
Posted by Sebastien Lachance on May 12, 2008
Like promised, on a previous post, I will show you how to mock HttpRequest. I will use Rhino Mock but you can use the one you want (moq, typemock, etc).
On you controller base class, a property called Request is exposed. Behind the scene you are accessing an HttpContextBase instance (this class is contained in the System.Web.Abstractions library), which is providing you the HttpRequest you want to mock.
So, let’s start by creating a mock of these two object.
var mocks = new MockRepository();
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
The mockedHttpRequest will be provided by the mockedHttpContext. So we will setup the mocked HttpContextBase.Request property to return the mocked HttpRequestBase.
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
But, what would we do with a mocked HttpRequestBase? When you need to test an action on a controller that will retrieve values for the Request.Form property you could create a NameValueCollection and tell the Form property of the HttpRequestBase instance to return it.
NameValueCollection formParameters = new NameValueCollection();
formParameters.Add("txtUsername", "username");
formParameters.Add("txtPassword", "password1");
SetupResult.For(mockedHttpRequest.Form).Return(formParameters);
I really like playing with the ASP.NET MVC framework!!!
Posted in .NET, ASP.NET MVC, Learning, Tests | No Comments »
Posted by Sebastien Lachance on May 8, 2008
I just got a hard time figuring how I could be mocking the HttpContext so that I would be able to mock HttpRequest. But once you understand the principle, it is fairly easy.
First thing we need to know is that the HttpContext is held inside a ControllerContext object. Once we have instantiated one, we can then put it inside our controller.
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
Where mockedHttpContext is your mocked HttpContextBase object.
Inside the HttpContextBase object reside a lot of useful stuff that can replace with our mocks. By example, the HttpRequest, HttpServer, HttpApplication, etc, are all contained inside. So, getting familiar with this basic knowledge is a must to do testing usefully.
Next post : Mocking HttpRequest.Form to return what we want.
Posted in .NET, ASP.NET MVC, Learning, Tests | 1 Comment »
Posted by Sebastien Lachance on January 24, 2008
I have always been told that TypeMock was too powerful, not strongly typed and for that, It shouldn’t be used. I believed that. The problem is : I should have given it a try before taking that for granted.
I have watched the Roy Osherove screencast and was impressed! I am currently working on an existing code base that was done before we went Agile and even before we were doing object-oriented development. When I need to refactor certain features I usually start writing tests and then using dependency injection to be able to mock some object. The minimum to be testable with Rhino Mocks. But with TypeMock you can mock object and don’t even need to inject them. You can also make expectation on static methods. Isn’t that just great! I believe so.
However, I will stick with Rhino Mocks for now. I like the way It force me to decouple my code and in some sense, it provide me with guidance to make my work testable.
P.S. : This is just my opinion and not the absolute truth.
Posted in Agile, Design, Tests, Tools | No Comments »
Posted by Sebastien Lachance on January 14, 2008
Sometimes, you come across real treasure! This is one of them, printed and with me for a long time.
I have just finished reading the “Foundations of Programming” series by Karl Seguin. This is a “Must Read”. It’s a really great series that cover all aspect of building an application. I can’t recommend it enough!
Here is a link to the PDF file which has all seven posts.
Posted in Agile, Continuous Integration, Design, Learning, Tests | 1 Comment »
Posted by Sebastien Lachance on January 8, 2008
For the getter, it’s exactly as you would do with a method :
Expect.Call(mockedConnection.CompanyConnectionString).Return("testconnectionstring");
For the setter, only assign the value you want to the property of the mocked object :
mockedConnection.ConnectionString = "testconnectionstring";</PRE
Posted in Agile, Design, Learning, Tests | No Comments »
Posted by Sebastien Lachance on December 18, 2007
Let’s not go on too much complicated stuff. This is an easy step-by-step to add a coverage report to your automated builds.
Andrew Stopford’s Weblog
Posted in Agile, Continuous Integration, Tests | No Comments »
Posted by Sebastien Lachance on August 3, 2007
Here is the second part of the Getting Start with Selenium IDE series. This part mainly cover the start of the test. The next part will feature the fun part, testing everything! Enjoy!!!
Structure of an html test
An html test is basically an html table. Each row is a command and each column has a specific purpose.
| Command |
Target |
Value |
| clickAndWait |
link=Open |
|
| type |
txtUser |
MyUsername |
| type |
txtPassword |
MyPassword |
The Command column contains the command to be executed. Nothing more. The Target column is the element on the page on which the command will be executed, but sometime it can have an other purpose like the pause command. And finally the Value column contains the value on which something will be tested. By example, you can have the assertNotText command and the value column will be the text that should not be there.
First command of the test
With that being said, let’s start with some concrete stuff. First of all, you will want to open your website. I recommend to use the Base URL text field, in case the root of your web site change. Enter your URL in the base URL text field. Then, add the first command of your test, which will be open, the target will be /startpage.html (replace startpage.html by your start page or leave it empty). Execute the test to verify that you are on the wanted page.
Command explanation
A command is a JavaScript function. Another way to see at the Target and Value column is that each one is a parameter of the function.
Use the reference tab in your Selenium IDE to see which parameters are needed.

Command action and ActionAndWait
There is various command available and much of the action have an equivalent with the AndWait suffix. The action without the AndWait suffix will be executed by the test runner and go directly to the next. However, there is scenario where a postback will occur after a command and the page will no longer be available for seconds. This is where you need to use the command ending with AndWait. Then the action will be executed and when the page has returned from the server the next command will be executed.
Selenium IDE Getting Started Series
Great resource
Posted in Agile, Tests | 4 Comments »
Posted by Sebastien Lachance on July 11, 2007
As promised, I will return back everything I have learned about Selenium. I invite you to go the OpenQA web site for additional information.
If you stick with me for this series of article, you will get a great quick start to create good acceptances tests.
Selenium IDE (Integrated Development Environment) is a Firefox extension that records your actions on a page. Then you can execute them back with the IDE or with other version of Selenium (Selenium Remote Control is one of them).
First step : Download Firefox
Second step: Install Selenium IDE extension on your browser
(If , for some reason, the Selenium IDE can’t replay your test, switch to a previous version.)
Third step : Record your first test
- In Firefox, go to Tools\Selenium IDE.
- Make sure the record button is pressed.
- Navigate on the web application.
- Press the green arrow (start button).
- Your first test is done.
Now, maybe the replay has not worked and you got some errors. This will be covered in the next post when I will show you how to add others commands to your Selenium test.

Here is some features found in the interface:
- There is the baseURL text field. Suppose you have two times the same web site, you want to have this attribute because you will avoid hard coding the path in the test. Simply specific another baseURL and the test will be performed on it.
- Run/Walk/Step. This is basically the speed at witch the test will be executed. However, Step will require you to press the “Step” button to go line by line. Very useful.
- Of course you can save the test, juste press Ctrl-S or go to File/Save Test.
- Table/Source tabs. Basically, a test is just an html file (not entirely true, you will see later), composed of a table element, and 3 columns (each one has a specific purpose).
- Play with Selenium Test Runner. This will launch a test runner for your test (will be covered later).
A lot more is possible and this is only the beginning.
If you find missing information, just leave a comment, and I will update the post.
Selenium IDE Getting Started Series
Posted in Tests | 4 Comments »
Posted by Sebastien Lachance on June 15, 2007
Did you know about a way of automating your UI test on your web application ? Next week, I will take a moment to do a series of posts about a tool called Selenium, so you will be able to bypass the long learning process I got into and avoid the problems I had encountered.
You can visit the Selenium’s web site at this address.
Posted in Agile, Tests | No Comments »