How to use NUnit GUI with a C#/ASP.NET website? - c#

I have a C#/ASP.NET website that has some code (*.cs) files in the App_Code directory. I would like to test them using NUnit. I have written a test file with the proper [TestFixture] and [Test] annotations and have put it here: App_Code/Test/TestClassName.cs.
I load up the NUnit GUI to run it but it wants me to select a .exe or .dll file. There is none in the bin folder of my project. My project does successfully run and is built and everything, but still no exe or dll file. How can I get the NUnit Gui to just run the test in that class?

I don't recommend putting test code in the same package you'll be deploying to production.
You may want to move the test classes to a library project, something like Business.UnitTest(there may be a built in way to create an nUnit specific project, if there is, use that). Then move the business classes that are in your App_Code directory into another project called Business. Have Business.UnitTest reference the Business library. That should get nUnit to run(I don't believe that nUnit runs against websites, only libraries, but I'm not 100% sure).
For your website add a reference to the business library you just created so your production code can access the business objects in the business library. You may have to work out some namespace issues, but that shouldn't be too bad.

The trick with .NET Website projects is that the code files are not normally compiled up front, they are compiled on execution of the respective pages. This presents a challenge where testing is concerned, since as you mentioned NUnit wants to run a .exe or .dll for testing.
One way to deal with the issue is to convert the website project to a web application; they sound similar, but work in different ways. In contrast to a website not requiring up-front compilation, a web application requires it. So you would have one or more projects that compile to assemblies (.dll) or executables (.exe). NUnit could then hook into those to run the tests.
To make this work, you would want to separate testable code into another project; your front-end web application can refer to this other project to make use of the code within. Ideally, the front-end would be a thin layer of logic and user interaction, and the real work can be sent to the second project. Therefore, the second project is what you will want to test.
You'll want to have one more project to contain the tests - general wisdom is to not have your tests in the same project as the code being tested. This project refers to the project being tested, and to NUnit, and contains the tests themselves. This assembly is what you would direct NUnit to run for testing.

First, you want to create a new project for your tests. If you happen to have any internal classes or attributes, you should use InternalsVisibleToAttribute in order to be able to test these from within your testing project, outside your "real" project.
This attribute is suitable for the entire assembly, so I recommend putting it into your Assembly.info file of your "real" assembly.
From within your tests project, add a reference to your "real" assembly.
Make sure to exactly know the path to your binary (assembly.dll);
Open your TestsProjectAssembly.dll from within your NUnit GUI, makeing sure you are browsing to the right folder;
You might also want NUnit GUI to reload your assembly on each tests run (there is an option for doing so in the options properties);
Run your tests.
Absolutely make sure your path or browsable folder is the one in which your testing project is generated.

Related

Why doesn't building my UWP Unit Test project produce a test dll

I'm trying to run unit tests from the command line.
For this, I need the test dlls, as per this:
https://msdn.microsoft.com/en-us/library/ms182490.aspx
In the Prerequisites section on that page, it says I need to "Run a Unit Test and Fix Your Code"
Question 1: Do I need to run tests from VS to be able to run them from command line? Surely not?
Now to the more important bit. When I build my solution, regardless of whether it's via VS or command line using MSBuild, I do not get any dlls generated for my tests.
I know this is for C++ but thought might still be relevant https://social.msdn.microsoft.com/Forums/vstudio/en-US/a89c2173-90e6-47b2-af8e-48865969cbca/msbuild15-does-not-create-a-dll-file-after-building-the-c-native-unit-test-project?forum=msbuild. (not that it helped of course).
Question 2: Why don't my unit test dlls get generated when building?
UWP is a bit different from other projects when it comes to Unit Testing. Universal Windows Apps run in a sandbox to make sure they don't do things they don't have permissions for. For this reason, the unit test project is not a simple DLL only, but in fact a full-fledged UWP app (and the generated executable has appx extension), that is launched and performs the tests.
That said, you can still launch the unit test project from the console using a special command as you can see in this SO answer.
vstest.console.exe /Platform:x64 AppPackages\UnitTestProject1_1.0.0.0_x64_Debug_Test\UnitTestProject1_1.0.0.0_x64_Debug.appx

TFS Unit Testing Build Questions

I have a solution that has 4 projects in it. 3 are dependencies for my tests and the other is just my tests.
DL
BI
MySite (web site)
MyTests
Some unit tests in the MyTests project reference namespaces in the web site MySite for some MVC contollers.
Question is how do I get just the MyTests project to build and deploy with a TFS build. NO matter what I try the _publishedWebSites folder on the TFS build machine always has the web site and not the MyTests folder. For some reason it thinks it is building the web site and not the tests. Any help would be appreciated from the build definition or solution perspective.
The purpose is to build the tests and distribute them to a server where they can be run (selectively) using the command line tool in the task manager. I cannot distribute them if I cannot get the solution to build properly.
Alright so there are a few things. Firstly, you need to make sure that the outer solution recognizes MyTests as the start up project and has the other projects as build dependencies. However, this likely won't solve your problems. In order to do this you'll probably have to create a custom build script or edit your solution/project files by hand. The problem with the latter approach is that if other people are building MySite from this solution editing the project file to exclude it's output from the drop is going to cause problems for them.
My personal approach would be to make an MSBuild script which specifies the order in which to build the projects and which files you want in the drop. It's fairly straight forward and it will probably be easy to specify the output you want (this is sometimes very tedious if the projects build is messy to begin with or it has excessive and convoluted dependencies).
Here's the outer most resource for MSBuild. I'd look it over and think about what the simplest solution is but I wouldn't be surprised if you can just make every project build using their project files then add a single build step to cleanse your output.

Using Nunit without creating separate project

I am creating one web application which has structure as shown in following image:
I have all entities and datastore files in the App_Code folder. i.e no separate project layer is created. I want to use Nunit for unit testing. But as for NUnit testing I need projectname.dll and my web application will not create dll I don't know how to test my methods.
If I create a separate project for unit tests, I can not reference datastore and other files in that project. Kindly suggest how can I use Nunit in this case.
Please, check this: Unit Testing ASP.net Web Site Project code stored in App_Code.
You can also create another project, just for unit testing purposes. When you create it, just include all the required files (in App_Code) As a Link. That way you will be creating shortcuts, so if you modify one file in your project, the other project you created will reflect the changes.

Referencing Main Project from a separate NUnit Test Library

I'm brand new to Unit Testing, fresh out of college, and suddenly responsible for developing and maintaining a fairly large application by myself. The previous developer (who just left the company) included NUnit with the project references, wrote a few empty test fixtures at the bottom of a couple classes, and never did anything else with it.
I'm afraid if I don't start writing unit tests now, while I'm refactoring and learning the system, I'll never get them done, but I'm having trouble understanding how to properly set up my test project.
I was told that I should keep my test project as a separate Class Library within the same solution, so I've made an OurApplication.Test Library project. I don't know how I reference the project that I'll be testing within it, though.
Guides online say to point it to my main project's .DLL... but there isn't one. Our project is a standalone application that doesn't generate a dll, and I'm not sure what I'm supposed to do in this case.
Any advice on what's wrong here, or pointers to more comprehensive guides would be greatly appreciated. I'd like to get this done the right way, and as soon as I can.
If you're using Visual Studio you can add references to Projects within the solution as here referencing a console application from a test project
Ideally it is best to separate your applications into separate dlls which based of functionalities , I.e. separating the solution into modules represented by projects of type class libraries. This will make it simpler to test these dlls separately. I usually have separate test project for each module (projects).
Having said that you can add a .exe project as a reference to your nun it project and get access to all namespaces and classes to test, though not best practice for maintainable code.

Possibly a conflict with file names of .Net and Silverlight assemblies (TFS Deploy)

Following problem: we're using gated build with TFS 2012. As soon as the TFS starts the build, everything is just copied into one folder.
Our actual application is a Silverlight application.
Our unit tests though are put in .Net projects
One part of our application uses 3rd party controls. It uses a Silverlight assembly, which grants a certain functionality. The same functionality is given in a .Net assembly, which we use in a parallel project for server-side stuff and unit testing. It actually grants the same functionality (except the UI things). Both of those assemblies are referenced with "copy local = true".
If I build the solution locally (which contains both assemblies referenced in different projects), everything works fine. But as soon as I try to deploy it via TFS, the build fails because of a FileIOException or something. It tells me that the 3rd party assembly is not the proper file.
I think that occured, because when the TFS starts building, everything gets copied into one folder. Both: the .Net assembly and the Silverlight assembly. They both have the same file name. I think that's, what messes up the TFS build.
Do you have any suggestions for this problem? It's a requirement that we still have the "uber solution", which contains all the projects. So splitting up .Net and Silverlight projects isn't a solution.
It is probably due to when you are building through TFS or some Non-IDE builder you are somewhere having an output directory specified so its just collating all assemblies in one place, causing an issue.
You can either append a suffix to your silverlight project assemblies, which will just make the assembly output as "YourAssembly.silverlight.dll" rather than "YourAssembly.dll", it would not mess with namespaces internally, although this can be confusing to other people unfamiliar with your system, generally you would just use namespaces to split them out so your silverlight framework would be within a *.silverlight namespace which paired with your project name indicating the namespace difference would solve your problem.

Categories

Resources