Skip to main content

Posts

Showing posts with the label unit test

Is there such a thing as too much unit testing?

Testing, Unit Tests, TDD (Test Driven Development) are one of the first thing that we learn when we are in university and study computer science. A part of us might be lucky enough to have a specific course for TDD. Code is written by people for machines that needs to resolve problems. People do mistakes and this is why it is so important to test our code. I didn’t have the opportunity to see until now a working application, without bug or issues from the beginning. I would say that a complex application that is not covered by unit tests and without a testing process will end up in the trash. At the beginning of each project there is a classical discussion related to unit tests and code coverage. - Do we need unit tests?  - Yes. - What is the code coverage target? - 80%, 60%, 20%, 100%.... It is pretty clear that we need unit test. An engineer needs to be able to test his code and check if what we develop is working as expect or not. The response for the first questio...

Visual Studio 2015 plus Windows 10 - Solution for 'System.TypeInitializationException: The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment'

It seems that the below Exception is following me like a ghost with each new version of Visual Studio. System.TypeInitializationException: The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception. ---> System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain. ---> System.Runtime.InteropServices.COMException: Invalid operation. (Exception from HRESULT: 0x80131022) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at <CrtImplementationDetails>.GetDefaultDomain() at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie) at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport*) at <CrtImple...

OWIN, OAuth - Bearer tokens: Authentication and Authorization for unit tests

It is a fact that nowadays, SPA applications are used in almost all new web project. In general behind a SPA application we have multiple REST endpoints that are used to get data, execute different actions and many more. Token base authentication is worldwide use when we need to secure a REST endpoint and offer a mechanism for authentication and authorization. In general when we create a SPA application using AngularJS we will use token based authentication. This mean that we will have an endpoint called ‘token’ (or other path) where users can send their username and password and receive a token that can be used to access different resources. Once we have the token, we will set the bearer token value of the authorization heather. Beside this we need to be able to write unit tests or integration tests that hit our REST services and validate their functionality. For this purpose we need to have a code that simulates the authentication step and inject in the REST request th...

Unit Tests fairy tale

A few weeks ago I had an interesting discussion with a college that is working in another company. I will try may a short summary of their story. Why? Because I was shocked to find out that…. unit tests are written only to give the team something to do. THE story They had to develop for one of their client a web application. They had a team of 4-5 people that worked on the project for 2 years and a half. In this period of time, they didn’t written NO unit tests. After this period of time, the development part ended and monitoring and supported phase started. The team reduce two 2 people that started to make bug fixing, adding small new features. Things like that, normal tasks for this state of a software product. Because the two members of the team didn’t had enough load a bright ideas came from the ‘God’. When you don’t have issues or tasks in the queue, start writing unit test. Of course, unit tests helped them to discover issues in the application. On top of this, they r...

Running Load Tests on Microsoft Azure with IP Switching feature

One year ago I started to investigate how we could use Load Test feature that is available on Visual Studio 2013 Ultimate and Microsoft Azure on our own project. The main idea was to develop a mechanism to test complex scenarios (especially written in C#) that can simulate 10k, 20k and even 100k users. The idea was accepted by client and we end up with a great and interesting application that can block a system witch worth millions. This is great from both sides. Mission complete for the team that defined the load tests, because they were able to define the load tests. But in the same time great for the development team and especially for the support team because they know the limits of the system. Now, we stuck with another problem. Because we are using Microsoft Azure infrastructure to run Load Tests (having our own hardware would be to expensive), we cannot simulate calls from multiple IP address. This is a current limitation Visual Studio Online - IP Switching feature is not yet...

Running unit tests using MSTest from console application

There are times when you end up with a requirement like this: You should be able to run Unit Tests from console application. And yes, we are talking about MSTests. Well, this is not very complicated using MSTest.exe Mstest /testcontainer:[pathToOurTestComponent.dll] After a few day you end up with a new requirement: Run Unit Tests using a console application from a machine that doesn’t   have Visual Studio installed Using different scripts from internet (like this one: http://mindinthewater.blogspot.ro/2011/02/executing-visual-studio-2010-unit-tests.html ), you end up with a configuration that can be used to run unit tests on machines that doesn’t have Visual Studio. Mission complete? Yes. But, you have a problem. Yes, there is a but. What about licensing?  Are you allowed to do something like this? Depends of the person who run your console application. You are allowed to do something like this as long the person who run the console application has a Visual Studio ...

JavaScript, Unit Test, Visual Studio and Build Machine Integration

Today I will write a short post about JavaScript and unit testing. I heard a lot of .NET developers that they didn’t wrote code for JavaScript because is not supported by Visual Studio, is complicated to run it on the build machine or they don’t have time. Guys, I have news for you, Visual Studio 2012 supports unit tests for JavaScript, even Visual Studio 2010. You can run them almost like a normal unit test for C# without needing to install anything. The JavaScript unit tests are so smart that are integrated in a way that you don’t need to change/install anything on your build machine – you even receive the standard message notification when a unit test fail. You don’t have time for them – I will not comment this, definition of DONE is wrong for those developers. When I need to write unit tests for JavaScript code I usually prefer qunit. Why? Because in combination with a small NuGet package called NQunit you can make magic. qunit give you the possibility to write and run Java...

How to write unit-tests for async methods

All developer that works with .NET heard about Task, async, await – Task Parallel Library (TPL). Great library when we need to write code that runs in parallel. With TPL, writing code that run in parallel is pretty simple. This is great, but of course, all code that run in parallel need to be tested also – unit tests. Do you know how you need to write unit tests for async calls? I so pretty strange way of unit tests for async methods. Some of them were ugly and complicated. Why? Because the unit test method is a sync one and there we try to run and wait a response from an async call. This is why we can end up with something like this: [TestMethod] public void MoveFile_ExistingFile_ResultsFileMovedAndOriginalFileDeleted() { StorageFolder destinationFolder = null; Task.Run(() => destinationFolder = CreateFolderAsync(_originalFolder).Result) ...

How to start Windows Azure emulator when running unit tests

Unit test need to be written even if we are working with on-premise services or with services from cloud. When working with Windows Azure, there are times when we want to write some integration test for Windows Azure tables, blobs or queues. For these cases we don’t want to hit the Windows Azure from the cloud. This would increase or monthly subscription costs. Usually for this this Windows Storage emulator is used in combination with development storage account. When we are on a development machine, where we already have Windows Azure Emulator stared we will not have any kind of problems. But will happen on a machine where Windows Azure Emulator is not started. All the tests will fail. We can write a code in the class initialize step that star the emulator. In the end, the emulator is only a process that can be started from the command line. The code that we would need to use will look something like this: public class CloudStorageEmulatorShepherd { public void...

New Unit Tests features of Visual Studio 2012

Visual Studio 2012 was already lunched some time ago. A lot of new feature were introduce with this new version. Some changes from Visual Studio 2012 are big and from some point of view, the difference between the old version of Visual Studio and the new one are like the difference between Windows 8 and Windows 7.   I think that a lot of developers already played a little with this new version of Visual Studio. The first thing that you notify after installing it is the time that is necessary to load the new Visual Studio – that is very low. The second thing is the new UI, that is Metro like. One zone where Visual Studio 2012 made a big step forward was Unit Testing. A lot of new feature were introduce in this area that not only increase the productivity of the developer, but also increase the quality of the code. In the next part of the post we will discover what are the main new features that were introduce from the unit testing perspective. Run tests after each build ...

How to write unit tests in JavaScript for a Windows Store Application

Did you ever tried to write a Windows Store Application for Windows 8 in JavaScript? If yes than I hope that you started working on unit tests also. If no, than in this post we discover how we can write unit test for JavaScript and how we can integrate them in Test Explorer. First of all why we need to write unit test for JavaScript? Because is like any other programming language that we used until now. When we write code in any language we need to test it. The framework and steps that are describe in this post can be used to write unit tests for JavaScript for a ASP.NET application of for any other project written in JavaScript. To be able to write test in JavaScript we need to define our test methods that contains a collection of checks. We can write this from scratch or we can use a framework. For this purpose I recommend qunit.  This is a great framework that can be used to write unit tests. We have different version of qunit for Windows Store Application and ASP.NET. After y...

Unit test classes - using a base class

Mai mult ca sigur cu toÈ›ii am scris teste. 1,2, 3 .. n clase de teste. ÃŽntr-un anumit moment ajungem sa dorim sa refactorizam codul, iar o parte din logica (setup-ul testelor) sa îl ducem în clasa de baza. public class Test1: BaseTest {     [TestMethod]     public void Method1Test()     {         Console.WriteLine(&quot;Method1Test&quot;);     }     [TestInitialize]     public void TestInit()     {         Console.WriteLine(&quot;TestInitialize&quot;);     }         [TestCleanup]     public void TestCleanup()     {         Console.WriteLine(&quot;TestCleanup&quot;);     }         [ClassInitialize]     ...