Testing the ASP.net module Website Project code stored in App_Code

I have an ASP.net website project (.net 3.5). Currently, all code-free code files (including Linq2Sql material, data contexts, business logic, extension methods, etc.) are located in the App_Code folder.

I'm interested in introducing Unit Testing (using nunit) in at least some sections of the project that are moving forward. Any module testing that I will do should have full access to the entire code, which is currently located in the App_Code folder. So far I have done a preliminary reading, and there seems to be consensus:

  • This is not possible for my current setup.
  • Testing modules requires references to classes that are part of a compiled dll, and a website project, by definition, is compiled only at run time.
  • To continue, I will either need to convert the entire project to a web application, or transfer all the code that I would like to test (that is: the entire contents of App_Code) to the class library project and the class library project link in the website project. Any of them will provide access to the classes that I need in the compiled dll format, which will allow me to Unit Test them.

It is right? Or is there another way I can Unit Test without restructuring / reorganizing my entire project?

+44
unit-testing nunit web-site-project
Jul 29 '09 at 7:29
source share
8 answers

Your conclusions seem correct. I would vote for moving the functionality to one or more projects of the class library, as this may open the door to reuse the same functionality in other projects.

+18
Jul 29 '09 at 7:38
source share

My store finally worked out the answer to this question for our MVC project. And I want to share it, since I have been chasing a lot of dead ends here, at the StackOverflow hearings, many say that this is not possible. We do it like this:

  • Open the MVC folder "as a website, from local iis", which works correctly with intellisense and debugging.
  • Add the unit test project, which is located in our controlled source directory
  • Add the pre-build step to the TEST project, since we cannot add it to the project opened as a website. Imagine the website \ FooSite and our test project is \ FooSite.Tests. The compiled application code will be in FooSite.Tests \ FooSite_Precompiled \ bin. *
<Target Name="BeforeBuild"> <AspNetCompiler VirtualPath="FooSite" TargetPath="$(ProjectDir)\FooSite_Precompiled" Force="true" Debug="true" /> </Target> 
  • Add a link to the FooSite_Precompiled / bin / App_Code.dll file in the test project.
  • Boom it. You can get your cake and eat it too. Each time you click Create in your solution, you call the aspnet_compiler.ext tool on your csproj website (which still exists), which, unlike MSBuild, compiles app_code, and Debug = "true" allows you step in the app_code.dll code when debugging unit test. And you only need to create when you run the updated unit tests. when you look at the effects of your changes on the page, you simply Change the code / Save / Refresh the page from the moment you dynamically add the folder app_code compiles when called from your web server.
+21
Apr 30 2018-12-15T00:
source share

We have this problem in my company (my boss doesn't like DLLs, some junk files about version control ...)

We have two paths around it that we often use:

1) Get the CI tool for unit testing: we use TeamCity, which has a rather complicated integration with NUnit, and our solution runs quickly enough (and has quite a few tests) to make this an acceptable option.

2) Manually pre-compile and unit test the resulting binaries: it is quite possible to run the ASP.net/MSBuild compiler from the command line (as if you were doing the Publish assembly) and just unit test as a result of the binaries.

However, if you have the opportunity to split the code into binary files (class libraries) or simply using a web application, I would suggest this as a better alternative.

+11
Jun 21 '10 at 11:32
source share

If someone finds a Brian solution, here is the Website.targets file that you can include in the solution for unit testing. It (re) compiles the website only when the App_Code changes. Just add something like

  <PropertyGroup> <WebsiteName>MyWebsite</WebsiteName> <WebsitePath>..</WebsitePath> </PropertyGroup> <Import Project="$(ProjectDir)\Website.targets" /> <Target Name="BeforeBuild" DependsOnTargets="CompileWebsite"> </Target> 

to your .csproj by setting WebsiteName and WebsiteName , and you should be ready to go. Website.targets:

 <?xml version="1.0" encoding="utf-8"?> <!-- Target that compiles Website App_Code to be used for testing --> <Project DefaultTargets="CompileWebsite" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <AppCodeFiles Include="$(WebsitePath)\$(WebsiteName)\App_Code\**\*.*" /> </ItemGroup> <Target Name="CompileWebsite" Inputs="@(AppCodeFiles)" Outputs="$(ProjectDir)\PrecompiledWeb\bin\App_Code.dll"> <AspNetCompiler VirtualPath="$(WebsiteName)" PhysicalPath="$(WebsitePath)\$(WebsiteName)" TargetPath="$(ProjectDir)\PrecompiledWeb" Force="true" Debug="true" /> </Target> <Target Name="CleanWebsite"> <RemoveDir Directories="$(WebsitePath)\$(WebsiteName)\PrecompiledWeb" /> </Target> </Project> 
+6
Jun 04 '13 at 8:17
source share

It seems that this is possible, but with this App_code , but I would either move this logic to my own class library project or change the project type to a web application, as Fredrik and Colin suggest.

I always create my own ASP.NET projects as web application projects, not websites.

+2
Jul 29 '09 at 8:06
source share

And since the OP stated that it is also possible to switch to a web application project that, as I would say, would be cleaner, your pages might remain in the wep application project, you will have them in 1 DLL (verifiable). All your business logic, etc. Included in a separate library / class libraries.

+1
Jul 29 '09 at 8:00 a.m.
source share

Unit test classes can be stored in the App_Code folder without converting your project to a web application or moving your classes to a class library project.

All that is needed is the installation of the "Build actions for compilation" code files. This will result in a debugging and testing module for your website to output the DLL file.

Now, when you refer to your site project from the unit test project, the classes in the app_code folder will be visible.

Note:

Installing your .cs Build Action files in Compile will cause your site to generate a DLL file for debugging and unit testing. The .dll file will cause problems when debugging your site, since IIS will now find your code in two places, in the trash and in the App_Code folder and will not know which one to use. Currently, I just delete the DLL file when I want to debug.

0
May 26 '16 at 21:06
source share

I had to change Brian White's decision by adding the PhysicalPath attribute. Also, I am not using Default Web Site and had to change the VirtualPath property to the name of my site.

 <Target Name="BeforeBuild"> <AspNetCompiler VirtualPath="myIISsitename.com" PhysicalPath="$(SolutionDir)MySiteFolder" TargetPath="$(ProjectDir)\MySite_Precompiled" Force="true" Debug="true" /> </Target> 

As a result, the dll will be in MySite_Precompiled\App_Code.dll

0
Jul 19 '16 at 1:11
source share



All Articles