Thursday 27 June 2013

Beginning Unit Testing with NUnit in Visual Studio

How to start with NUnit?

Download it from http://www.nunit.org/ and install it. It should be installed at C:\Program Files (x86)\NUnit 2.6.2. (2.6.2 is the current release version in the time of writing this article)

How to organise my projects? Where to put unit tests?

Put the business logic of your app in a class library project (e.g. MyBusinessLogic.dll). Your main app (MyApp.exe) project references this dll. Put unit tests in a separate project (e.g. MyBusinessLogicTester.dll) but within the same solution. MyBusinessLogicTester.dll will reference nunit.framework.dll (located at C:\Program Files (x86)\NUnit 2.6.2\bin) and MyBusinessLogic.dll.

How to run NUnit tests?

In MyBusinessLogicTester project: go to Properties -> Debug. Set Start external program to C:\Program Files (x86)\NUnit 2.6.2\bin\nunit.exe and put MyBusinessLogicTester.dll in Command line arguments field.

Set MyBusinessLogicTester as a StartUp project in your solution and run it. Nunit GUI app (nunit.exe) will open and display all test fixtures and tests in the left-hand side tree view. You can select tests you want to run and then execute them.

If you're running MyBusinessLogicTester tests with ReSharper, make sure MyBusinessLogicTester is marked to be built in the Configuration Manager.


Links and References:
http://www.nunit.org/
http://stackoverflow.com/questions/67299/is-unit-testing-worth-the-effort
http://stackoverflow.com/questions/1365943/how-to-start-unit-testing-or-tdd
http://stackoverflow.com/questions/347156/do-you-put-unit-tests-in-same-project-or-another-project
http://stackoverflow.com/questions/3979855/how-do-i-set-up-nunit-to-run-my-projects-unit-tests
http://stackoverflow.com/questions/759854/how-do-i-run-nunit-in-debug-mode-from-visual-studio
http://stackoverflow.com/questions/3476054/can-unit-testing-be-successfully-added-into-an-existing-production-project-if-s
http://stackoverflow.com/questions/6103807/unit-testing-philosophy


Thursday 14 March 2013

Hello, world! in NSIS - the simplest NSIS script

NSIS requires two things to be present in a script:

1) call to OutFile function (in order to define installer's name and output directory)
2) at least one Section (NSIS script executes sections and each section can contain functions and instructions; functions contain instructions only)

So, the shortest script that compiles could be:

HelloWorld.nsi:




NSIS compiler creates HelloWorld-installer.exe in the same directory where HelloWorld.nsi resides. When run, installer looks like this:

NSIS-HelloWorld-1

If we click on Show details button, we can see the following:

NSIS-HelloWorld-2

We can use ShowInstDetails in order to make installer displaying details by default:



We can use DetailPrint in order to output our custom messages:



And so Installer displays our custom text and Details window by default:

NSIS-HelloWorld-3


If we want to perform some system checks when installer starts, we can place our code in predefined function .onInit. In the next example, we'll just put displaying message box:



When we run installer, message box is first displayed:

NSIS-HelloWorld-4

When we click on OK, main window shows:

NSIS-HelloWorld-5

I used instruction Name to customize title of Installer's windows.

We can do some more serious stuff in .onInit function, like Windows version check. Download GetWindowsVersion.nsh header, create directory headers in the same directory where HelloWorld.nsi is, place GetWindowsVersion.nsh in that new directory (as we want to keep downloaded scripts separately from our work), include that header in the script and call function it exposes - GetWindowsVersion:



Message box displays Windows version:

NSIS-HelloWorld-6

NOTE: Files with extension nsi are installer script files and files with extension nsh are installer headers.

NOTE: DetailPrint calls from .onInit are ignored.

Thursday 10 January 2013

How to issue periodical execution of DOS command in a single line

Let's say we want to check the state of the socket connection on the port 4016 every second. We can use netstat command to display required information and ping invalid IP address with timeout of 1 second. These two commands can be grouped in the same line with & operator and that group can be put inside infinite FOR loop:

Command:
C:\Users\Scrillex>for /L %a in (0,0,0) do (netstat -an|find "4016" & ping 1.1.1.1 - n 1 -w 1000 >nul)

Output:
C:\Users\Bojan>(netstat -an | find "4016" & ping 1.1.1.1 -n 1 -w 1000 1>nul)
TCP 0.0.0.0:4016 0.0.0.0:0 LISTENING
TCP [::]:4016 [::]:0 LISTENING

C:\Users\Bojan>(netstat -an | find "4016" & ping 1.1.1.1 -n 1 -w 1000 1>nul)
TCP 0.0.0.0:4016 0.0.0.0:0 LISTENING
TCP [::]:4016 [::]:0 LISTENING

C:\Users\Bojan>(netstat -an | find "4016" & ping 1.1.1.1 -n 1 -w 1000 1>nul)
TCP 0.0.0.0:4016 0.0.0.0:0 LISTENING
TCP [::]:4016 [::]:0 LISTENING
^C

To stop the execution of this single line script use CTLR-C.