background preloader

Unit testing

Facebook Twitter

‪Practical Unit Testing 2014‬‏ Unittest introduction - Python Testing. The unittest test framework is python’s xUnit style framework.

unittest introduction - Python Testing

It is a standard module that you already have if you’ve got python version 2.1 or greater. In this post, I’ll cover the basics of how to create and run a simple test using unittest. Then I’ll show how I’m using it to test markdown.py. Overview of unittest The unittest module used to be called PyUnit, due to it’s legacy as a xUnit style framework. The standard workflow is: 1. One of the many benifits of unittest, that you’ll use when your tests get bigger than the toy examples I’m showing on this blog, is the use of ‘setUp’ and ‘tearDown’ functions to get your system ready for the tests. Automated Unit Testing Software - Typemock. How to mock static class or static member for testing. One of the problems with static members or static classes is that you can’t mock them for proper unit-testing.

How to mock static class or static member for testing

Instead of taking this fact for granted, let’s demonstrate it. Assume that we have the following classes (please note, this is just an example, not production code or anything that I’ll be proud of later on): public class CsvDataExtractor { private string _documentPath; public CsvDataExtractor(string documentPath) { _documentPath = document; } public string ExtractFullName() { string content = CsvDocumentReader.GetContent(_documentPath); string fullName = // extract full name logic return fullName; }} This test is not good as we can’t test the ExtractFullName by itself - we’re testing that CsvDocumentReader.GetContent works as well.

Here are our options to solve this dependency so we could test ExtractFullName method by itself: 1). ASP.NET OOP and Unit Testing. ASP.NET is a very functional framework for developing web applications.

ASP.NET OOP and Unit Testing

Creating an object-oriented solution is harder to do so because of the coding effort involved with doing so. Unit Test Web Code Without A Web Server Using HttpSimulator. Testing code written for the web is challenging.

Unit Test Web Code Without A Web Server Using HttpSimulator

Especially code that makes use of the ASP.NET intrinsic objects such as the HttpRequest object. My goal is to make testing such code easier. One ASP.NET Sneak Peek: Elegant Web Forms and Snowballs in Hell. Unit testing code behinds in ASP.Net Web Forms. 13 June, 2010 § Have you ever inherited an ASP.Net website with waaaaaaay too much logic in the code behinds?

Unit testing code behinds in ASP.Net Web Forms

A while back I came up with a solution to solving this problem. I don’t have a name for it, but if you do, please leave a comment at the end of this post. The problem that I was trying to solve is that I wanted to make a new page and implement it using Test Driven Development. The hard part was that there was a lot of code that basically dealt with the visual aspect and interaction of the page but not much with the data model. Here is an example of the before-case: Telerik JustMock Lite 2014.1.1519.1. Mocking HttpWebResponse with Moles. During the development of our DataVault™ service we came across some areas of the code that did not seem well suited to TDD.

Mocking HttpWebResponse with Moles

These were areas of the code which had dependancies on external sites that we needed to access using the HttpWebRequest / Response objects. As anyone who’s attempted to write code using these objects is aware they are intrinsically difficult to mock; HttpWebResponse has a private constructor and it leaves you with two options: You can use an intercepting proxy to fake the presence of the real web serverYou can create an abstraction of the HttpWebRequest/Response classes and use IoC to allow us to inject stubbed variants so that the method under test remains largely intact. Whilst both of the above methods will work they both have inherent issues. A is clumsy; in that we need an external process to intercept the calls and we’re not truly ‘unit’ testing the code; it’s turning into a little bit of an integration test. Download Microsoft Moles. Mocking HTTPContext object. Testing - Is is possible to unit test a web forms site.

Mocking framework, mocking tool, for easier unit testing. C# - How do you programmatically fill in a form and 'POST' a web page. Web Application Testing for .Net (WatiN Test Recorder) C# - Understanding unit tests/TDD for ASP.NET webforms. WatiN. ASP.NET - Unit Testing in the Navigation for ASP.NET Web Forms Framework. The Navigation for ASP.NET Web Forms framework, an open source project hosted at navigation.codeplex.com, opens up new possibilities for writing Web Forms applications by taking a new approach to navigation and data passing.

ASP.NET - Unit Testing in the Navigation for ASP.NET Web Forms Framework

In traditional Web Forms code, the way data is passed is dependent on the navigation performed. For example, it might be held in the query string or route data during a redirection, but in control values or view state during a post back. However, in the Navigation for ASP.NET Web Forms framework (“Navigation framework” hereafter for brevity) a single data source is used in all scenarios. In my first article (msdn.microsoft.com/magazine/hh975349), I introduced the Navigation framework and built a sample online survey application to demonstrate some of its key concepts and the advantages it provides. Unit Testing in ASP.NET MVC Applications. A significant benefit of using the MVC pattern in ASP.NET is that you can easily implement unit tests for your Web application.

Unit Testing in ASP.NET MVC Applications

This is particularly true in comparison to the ASP.NET Web Forms page model, where unit testing is more complex because it is difficult to isolate specific functionality, and because testing Web Forms pages requires that you invoke the Web server and run the complete page pipeline. ASP.NET MVC has been architected for testability without dependencies on the IIS server, on a database, or on external classes. When you create a new ASP.NET MVC project in Visual Studio, the Create Unit Test Project dialog box is displayed. If you select Yes and create unit tests, a test project will be created in your ASP.NET MVC solution that contains unit tests for the account controller and the home controller. How to: Create an ASP.NET Unit Test. How To Use Model View Presenter With Asp.Net WebForms. Writing unit tests for traditional asp.net code-behind code can be tricky.

The code is usually full of dependencies to UI objects and user interactions that present challenges when attempting to write unit tests. In this article I will describe how to reduce the impact of these dependencies by using the unit test friendly pattern Model-View-Presenter (MVP). This article assumes basic knowledge of MVP, so I recommend checking out the following Wikipedia article if you are new to MVP. In short, the main purpose of MVP is to separate pure UI code from the logic used to control the UI code. The benefit of this is much better testability, but also better code reuse. NUnit Unit Testing of ASP.NET Pages, Base Classes, Controls and other widgetry using Cassini (ASP.NET Web Matrix/Visual Studio Web Developer)

NUnitAsp - ASP.NET unit testing. My Life and Code: How to Mock an HttpContext for Unit Testing. I recently made some modifications to a class written by another programmer.

My Life and Code: How to Mock an HttpContext for Unit Testing

When I went to write a unit test for my new additions, I found that the class made use of the statically accessible HttpContext.Current object. For my unit test, I wanted to be able to mock this out and use dependency injection to pass the mock in. Here are the steps I took to accomplish this. First, a brief summary of the classes I worked with to get this done: HttpContext: The original ASP.NET HttpContext. Private HttpContextBase _httpContext = null; Then, I created a new constructor for the class to take an instance of HttpContextBase as a parameter, and assigned it to this variable: public MyExampleClass(HttpContextBase httpContext) { _httpContext = httpContext; } To pass in an HttpContext from a web page, you'd need to first wrap it in the aforementioned HttpContextWrapper class.

The mocked HttpContextBase then gets passed into the instance of the class I want to unit test. Fake/Mock HttpContext without any special mocking framework. When writing unit tests, we often need to find a way to mock concrete classes of the .NET Framework in order to make our tests truly unit tests rather than integration tests, and although there are frameworks which allows one to mock some of these classes, sometimes you just can’t (or don’t want to) take a dependency on one of these mocking frameworks.

Fake/Mock HttpContext without any special mocking framework

C# - Mock HttpContext.Current in Test Init Method.