Friday, March 24, 2017

Introducing NUnit on Visual Studio 2010 in Visual Basic

As part of my research on Xamarin I started looking at NUnit as a unit tester. I was curious to see how I would implement it on my Visual Basic Purchasing project. We are still using Visual Studio 2010 so the implementation details are a little different. But it can be done.

Let's start by installing NUnit.

Installing NUnit

Start Visual Studio 2010
In the menu click Tools -> Extension Manager
On the left, click Online Gallery.
In the search area, enter NUnit and hit Enter.
Scroll down to "Visual NUnit 2010" and select it.
Hit [Download]
Restart Visual Studio


Creating the project to be tested

Start a new class library project and call it "Calc".
Rename "Class1" to "Add"
Add a public method called Add so the code looks like this.

Public Class Add

    Public Function Add(op1 As Integer, op2 As Integer) As Integer
        Return op1 + op2
    End Function
End Class

Build the project

Creating the test project

Right-click on the Add class and select "Create Unit Tests..."
Ensure only "Add(System.Int32, System.Int32)" is checked and click [OK]. You don't need to create a test for the class's constructor. When prompted for the new project's name call it "CalcTest".


Open the CalcTest project, open AddTest.vb. In the AddTest method you may need to change "Add" to "Add.Add".

Change op1 value to 1, op2 value to 2, expected to 3. Remove the Assert.Inconclusive line.

    <TestMethod()> _
    Public Sub AddTest()
        Dim target As Add.Add = New Add.Add()
        Dim op1 As Integer = 1 
        Dim op2 As Integer = 2         
        Dim expected As Integer = 3 
        Dim actual As Integer
        actual = target.Add(op1, op2)
        Assert.AreEqual(expected, actual)
    End Sub

Make CalcTest the startup project.

Run the application. You will see a Test Results window that looks like this.


Now go back to Calc.Add and change the Add method to replace the "+" with "-" like this.

Public Class Add

    Public Function Add(op1 As Integer, op2 As Integer) As Integer
        Return op1 - op2
    End Function
End Class

Rebuild and run again. The "Test Results" panel will now show an error.


Wednesday, March 22, 2017

Xamarin - getting started

Nothing to do with WPF except they both use XAML. I finally figured out how to get the whole emulator thing working. It's ridiculously difficult.

I installed Visual Studio 2017 Professional when Microsoft released it. It has better integration of Xamarin Studio than 2015 had. This made the process of creating my first Android App in Visual Studio much easier - but still complicated.

I also broke down and bought a book "Xamarin 4.x Cross-Platform Application Development - Third Edition" from Amazon.com. It's not a great book but it has more detail than the online examples I could find.

So here is the list of things I had to do to get my first app working.

I got my copy of VS2017 through my MSDN subscription. You should be able to get a free community version from Microsoft.

Setting up Visual Studio for the first time.

Start Visual Studio 2017 (any version)
Click on Tools -> Android -> Android SDK Manager.



Make sure that the following are installed or checked...
Tools/Android SDK Tools
Tools/Android SDK Platform-tools
Tools/Android SDK Build-tools (as many versions as you want - recommend 23.0.3)
Android 7.1.1 (API 25)/SDK Platform
Android 7.1.1 (API 25)/System images for all devices you want to emulate
Extras/Android Support Repository
Extras/Google USB driver
Extras/Intel Hardware Accelerated Execution Manager

Click [Install packages]

Close Visual Studio.

Now you need to install the hardware accelerator. In File Manager browse to C:\Program Files (x86)\Android\android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager and run intelhaxm-android.exe.

Open Visual Studio again. Now we are ready to develop.

Click on Tools -> Android -> Android Emulation Manager



You may see a different list of virtual devices.

Select "VisualStudio_android-23_arm_phone" and click [Start...]. This launches an Android phone emulator. You will see a "Launch Options" popup. Click [Launch].

You will now see a "Starting Android Emulator" popup. Make sure there are no errors listed. In the example below the only error concerns the audio which I don't care about. You can close it once the green bar is full.



At the same time the emulator will popup and once it has initialized it will look like a phone. It can take as long as five minutes to initialize. While it is initializing it shows the word "android".



When you run your program it will connect to the emulator and run there. It's quicker to have the emulator running before you build. However, the build will start an emulator if you don't have one running but it takes forever.

While the emulator is initializing we can start developing.

In the Visual Studio menu click File -> New -> Project. You will see the "New Project" popup.



In the left panel under Templates select Visual C# and then Android. In the right panel select Single-View App (Android). Enter a project name and click [OK].

This creates a single-view application with some minimal functionality that is useful for us but which will be removed most of the time. After Visual Studio creates the app your screen will look something like this.



You can explore the solution if you want or you can just build and run the application (hit F5). Remember we have the emulator running already showing the clock, icons, etc. If the emulator is not initialized (looking like the image above) you should wait until it is.

The process of connecting takes some time. Your output window and the status bar at the bottom of Visual Studio will show the progress. The status bar will show the following messages as the deployment progresses.

Starting deploy...
Deploying...
Android application is debugging
Ready

It takes fully two minutes for the emulator to start showing this program on my computer and this is a trivial program with the emulator already running. However, eventually, it looks like this.



Click in [HELLO WORLD, CLICK ME!] to test the application.

You have to understand how incredibly slow Xamarin is in its default configuration. I think a lot of the problems I was having is that I'm used to Visual Studio only taking a few seconds from hitting F5 to running even on massive solutions.

Are you old enough to remember the bad old days of batch compiles and builds when we had to wait five or ten minutes before we could start debugging? I'm talking about the HP3000 COBOL compiler and build engine. Xamarin has taken us back to the old days.