From the course: Bamboo Essential Training (2018)

Run unit tests

- [Instructor] In this topic we want to see how we can run Unit Tests via Bamboo. Because hopefully you have Unit Tests either after you write your code you add Unit Tests or you create your Unit Test and then you add code in case you follow TDD approach, either way you want to run your Unit Tests and make sure that all the Unit Tests are passed. Because as we mentioned before one of the things that we want to achieve with this continuous integration system with Bamboo is that, if multiple teams work on same code repository we want to make sure that first of all, the code compiles which we did it in Bamboo. And also after that we want to make sure that all tests including Unit Test and integration tests pass as well. Because one developer may make a change that may break someone else's test and that's an, it's kind of a red flag for the development team that like there is a breaking change there. So you will want to now run the Unit Tests. In order to run the Unit Tests, you need to have the Unit Test runner on your build Agents, or on your build Agent. What is a Unit Test runner? You use a kind of framework for you Unit Test, for example if you are are .NET developer you most likely use MSTest, if you use just what Visual Studio offers. Or you may use NUnit which is a more common framework for .NET and you may use XUnit, JUnit depending on what programming language you use. It doesn't matter, all these Unit Test frameworks come with something that is called a test runner. Test runner is a kind of a command line tool that is able to run your Unit Tests and generate a report while normally in an XML format. So in this example, and in this topic, we want to use NUnit on a .NET application and then run the Unit Test in Bamboo. If you don't use .NET all the concepts are the same you only choose a different test runner, that's the only difference. So as you know in our Bamboo Plan, we are building an ASP.NET application that is on GitHub and I call it Accounting but it's obviously not an accounting software it's just what ASP.NET creates when you create a new ASP.NET MVC application, that's it. And I'm just going to take you to Visual Studio which is here, and I'm gonna show you inside Visual Studio that the application has an ASP.NET application website and a Unit Test project in it and inside the Unit Test project I have a Home Controller Test which I double click on it, and you will see that in here I have used NUnit via GitHub. The reason that is red is that I have to Restore NuGet Packages. But as you see I have used NUnit framework, I have a class that has an attribute called Textfixture, Testfixture, sorry and there is a method which has an attribute called Test and all it does is that it creates, an instance of the controller calls the index method and make sure that the method is returning a view. So all it does is this, it's a very simple test. And if you are a .NET developer you need to go to your NuGet Package Manager and make sure that you have NUnit installed. If you use mocking you can have NUnit mocks as well. You don't need NUnit ConsoleRunner, there are two ways of having the test runner. One is that you get it via NuGet package, just like this and then you use the exe file one you download it from NuGet using the NuGet exe, another approach is that we install it on the build Agent which I'm going to do it in this topic. So you don't really need this, I added this for another purpose, explaining it in TMCT so that was a different story, you don't need it in Bamboo. What you need in Bamboo is just NUnit, and possibly NUnit Mocks if you do mocking in your tests. So that's all you need and then once you have everything you just check in your changes and everything is all good. And then we go back to our build Agent. This is my build Agent, I have remotely logged in to my build Agent which is a Windows server, as you know, so what you need to do in here is that you need to go to nunit.org/download which is a very common URL and that's why Google knows it. And when you are here, there are different options in here you can get NUnit, you can get NUnit Console, NUnit Test Adapter, what you need in here is the NUnit Console. If your application is new and it's not an old application that uses the old NUnit version two, make sure you always get the latest version of NUnit which must be three and above like 3.8 is the most recent version at the time of creating this topic. So what you need in here is to get the msi package because we want to install this on the Agent. So, I'm downloading it and then I'm gonna just run it and I click on Next, I accept the terms and conditions and in here, I'm just going to click on Custom Installation. Because I want to click on Browse and copy this path installation path into clip board, and I click on OK, Next, Install, it's a very quick installation. And then from here just go to Windows Explorer, I paste that path in here and as you see, this is my NUnit installation and there is a sub-folder in here called nunit-console, I just open that and I need this path to be in my clipboard. That's it, and then to, in order to configure our build Agent it's much better to do it while we are logged in to build Agent because all the copy pasting stuff works much better. So I will go to learnbamboo.me which is path to my Bamboo instance and I log in with my admin account and in here we need to go to Agents. It is very very very important that you install NUnit Agent, NUnit Console or NUnit Runner whatever you call it first and then you configure your Agent. If you do it the other way around you are going to get weird messages that although the test runner exists on your server, Bamboo might tell you that you don't have it and there is no Agent available for building your Plan. It's very important that you do it in this order that I am showing to you. So anyway what we do in here is that if you have one Agent, you can click on the Agent's name or IP in here and then add a capability to it. If you are using Called Agents, you need to go to shared remote capabilities in here. There is a link, and add your capability in here. When you add Shared remote capabilities, Bamboo assumes that all your remote Agents, be it a Agent that is on a different machine or it's a cloud Agent that gets spinned up whenever it's needed, Bamboo is gonna assume that the build Agent has this capability. So either you click on Shared remote capabilities and add your capability, or you just click on the Agent name or IP and then you add the capability in here. And the capability that you are adding is of type executable so either you click on Add Executable, or you click on Add capability and in here you choose Executable, they do the same thing. And make sure that in here you choose NUnit 3, not NUnit 2. So you click on NUnit 3 you give it a name for example, I'll call it Test Runner, and then the path is the path to my nunit3-console.exe file. And then I click on Add, that's it. Make sure that you do this on the Agent. Now if we go to our Plan, you can do it either on here, you can just go back to your local machine, like this and then we go to our Plan and then we go to Configure Plan and in here I click on the Default Job. So so far I have three tasks here, one is Source Code Checkout which is Git cloning my repository, the second one is a command which is NuGet restoring my packages. The third one is MSBuild which compiles my source code. After that, I'm going to add my test runner. So I'm gonna click on test, I'm sorry on Add task and then in here at the left side I will click on Tests and these are the runners that are available in Bamboo and what we need is NUnit Runner. Because that's what we installed on the Agent. If you for example, install and use MSTest what you need is MSTest Runner, so we click on NUnit Runner in here and you need to give it a name. For example we can say Test Runner NUnit, or if we want to give it a generic name you can say, Run Unit Test, something like this. And as you see because Bamboo had detected that capability in my build Agent it's being displayed in here and Test Runner is the name that we gave to that capability. Make sure that you always install the test runner on the server, then you add it as a capability to your Agent, and then you choose it in here. Do not click on Add new executable in here because if you do that it's gonna add it as a server capability not as an Agent capability. You don't need a server capability in here you need an Agent capability. Because you want to run the Unit Test on the Agent. So make sure that you add the capability to Agent and then you choose it in here in this drop down, and after that NUnit Test Files refers to your DLL files or to your C Sharp project, or to your NUnit project. So for this example, what I'm going to do is that, if I just right click on my test project and say Open Folder in File Explorer, we will see that the test project is in a sub-folder called Accounting.Tests and the project name is called Accounting.Tests.csproj, csproj so we need to put the name in here. And because Bamboo is a Linux kind of thing we need to put slash, forward slash not backslash and then we put our Accounting.Tests.csproj in here and the result of the tests can go to TestResult.xml the default file name it's all good. And then we click on save, it says Task created successfully. Now let's run this, run the Plan, and see what happens. I click on Run Plan it gets queued and then it should be executed in a few seconds. There you go, and this is the log. It's been executed, and it fails. As I said before failing a test, and failing a build is normal, so we don't get upset. We just go click on logs to diagnose the problem and the error is actually in here, a very obvious. It says that invalid file name, file not found, this DLL. Because although we gave the name of the project, NUnit tries to go and find the DLL file of that project and as you see it's trying to find it in \bin\Debug folder. But if we go to our Plan and we go to the MSBuild, you see that in here, configuration is bamboo.Configuration this is a parameter that we created if you remember. And if I go to again my Plan Configuration and then I go to Variables, the default value for this configuration is Release. That means that MSBuild is compiling my code in Release mode and it's basically putting all the DLL files in /bin/Release not /bin/Debug. So we need to use a Command Line Option which is in here there is a box, and it's a config command. Because in NUnit Runner you can pass a /config and then specify a release debug or any other configuration that you might have. So if I say /config is Release it's gonna compile it in Release. But because that variable is used for MSBuild the user can choose either Debug or Release, they can change it so it doesn't make sense to hard code it in here because now it's gonna work with Release configuration but if I do a custom build and choose Debug instead of Release, my tests are gonna fail. The best thing is to put the variable that we created in here, so now the Test Runner is being executed with the exact same configuration as MSBuild. So we click on Save. And we run the test again. Sorry we run the Plan again, and it's gonna run, compile the code and then run the Unit Tests. Perfect! Now as you see we expected this green color and everything is good, and if we go to Tests tab in here you see that is says 1 test in total. And if you want to see more details you click on the Default Job and it shows you the name of the test that was passed, and if I click on Index I get a bit of more information in here. So that's how you can run Unit Tests and validate the changes that different people are making on your source code. If you want to see more details, you can simply click on Logs, you know this already and you can read the logs. Before we end this topic I'm gonna actually show you something that you learned in the couple of topics before and that was the build, sorry the Plan for branches. Or branch Plans or Plan branches and as you see, once we activated the branch Plans this drop down appeared in here next to our Plan and it says that now you are working on master branch, everything that we do happens on master branch. If I go to MSTest I'm working on a different branch and it'll be a different story, everything will change unless things are being inherited from the main Plan. So you, I just wanted to let you know that this drop down in here an you can switch between Plans as well. So that was about Unit Tests, very easy, let's go and to the next topic and learn something new.

Contents