170

Do you put unit tests in the same project for convenience or do you put them in a separate assembly?

If you put them in a separate assembly like we do, we end up with a number of extra projects in the solution. It's great for unit testing while coding but how do you release the application without all of these extra assemblies?

16 Answers 16

136

Separate project, but in the same solution. (I've worked on products with separate solutions for test and production code - it's horrible. You're always switching between the two.)

The reasons for separate projects are as stated by others. Note that if you're using data-driven tests, you might end up with quite a significant amount of bloat if you include the tests in the production assembly.

If you need access to the internal members of the production code, use InternalsVisibleTo.

11
  • 1
    +1: I've just encountered unit tests in the same project as the main code and it is hard to find the tests amongst the real code - even though a naming convention has been followed.
    – Fenton
    Sep 13, 2012 at 10:43
  • 46
    For less experienced reader this means you add [assembly:InternalsVisibleTo("UnitTestProjectName")] to your project AssemblyInfo.cs file.
    – Margus
    Jun 18, 2014 at 7:13
  • Very useful addition Margus. Thanks to Jon for mentioning InternalsVisibleTo in this context. Aug 27, 2014 at 8:21
  • 1
    @jropella: If you're signing the prod assembly, you can only make the internals visible to signed assemblies anyway. And of course if you're running any code in full trust, they have access via reflection...
    – Jon Skeet
    Sep 30, 2015 at 21:44
  • 2
    @jropella: Reflection doesn't care about InternalsVisibleTo anyway... but you wouldn't have seen a signed assembly trusting an unsigned assembly using InternalsVisibleTo, because that's prevented at compile-time.
    – Jon Skeet
    Nov 12, 2015 at 6:55
117

In my opinion, unit tests should be placed in a separate assembly from production code. Here are just a few cons of placing unit tests in the same assembly or assemblies as production code are:

  1. Unit tests get shipped with production code. The only thing shipped with product code is production code.
  2. Assemblies will be unnecessarily bloated by unit tests.
  3. Unit tests can affect build processes like automated or continuous build.

I don't really know of any pros. Having an extra project (or 10) isn't a con.

Edit: More Info On Build and Shipping

I would further recommend that any automated build process place production and unit tests into different locations. Ideally, the unit test build process only runs if the production code builds, and copies the product files into the unit tests directory. Doing it this way results in the actual bits being separated for shipping, etc. Additionally, it is fairly trivial to run automated unit testing at this point on all tests in a particular directory.

To summarize, here is the general idea for a daily build and testing and shipping of bits and other files:

  1. Production build runs, placing production files into a specific "production" directory.
    1. Build production projects only.
    2. Copy compiled bits and other files into a "production" directory.
    3. Copy bits and other files into a release candidate directory, aka a Christmas release directory would be "Release20081225".
  2. If production build succeeds, unit test build runs.
    1. Copy production code to "tests" directory.
    2. Build unit tests to "tests" directory.
    3. Run unit tests.
  3. Send build notifications and unit tests results to developers.
  4. When a release candidate (like Release20081225) is accepted, ship these bits.
19
  • 25
    IMO the cons you list aren't always applicable. A pro for same-project is easier grouping of tested class + tests - these small conveniences go a long way when writing tests. Personal preference wins here, and sometimes your points are relevant, just not all the time.
    – orip
    Dec 7, 2008 at 9:06
  • 9
    You should first ask whether you need to remove the tests when you ship. If yes, you need a separate project. If not, use the other pros and cons to decide. People that assume they can't deploy the tests will always reach the "separate project" conclusion by default.
    – orip
    Mar 17, 2010 at 16:28
  • 10
    I don't see any upsides to shipping non-production code like unit tests, and there are plenty of downsides. Shipping unit tests means you are dealing with more bits that need to be distributed. Unit tests also have a separate set of dependencies. Now you are shipping NUnit, Rhino or Moq, etc. That is even more bit bloat. Placing unit tests into a separate project requires only a small amount of effort, and that is a one time cost. I am very comfortable with the conclusion that unit tests shouldn't be shipped. Mar 18, 2010 at 23:22
  • 7
    As an alternative to the "separate project" approach for removing unit tests in production code, consider using a custom symbol and compiler directives like #if. This custom symbol can be toggled using compiler command-line parameters in your CI software scripts. See msdn.microsoft.com/en-us/library/4y6tbswk.aspx.
    – Rich C
    Feb 7, 2014 at 3:35
  • 36
    .NET is behind the curve on having tests in a completely separate project, and I would bet that this will change, soon. There's no reason the build process could not be made to ignore test code in the release build. I have used several web app generators that do this. I think it is absolutely better to include unit test codefiles right alongside the codefiles they describe, peppered throughout the same file hierarchy. Google recommends this, called the 'fractal' file organization. Why are tests any different than inline comments and readme documentation? The compiler gladly ignores the latter. Jan 28, 2015 at 18:17
72

I do not understand the frequent objection to deploying tests with production code. I led a team at a small microcap (grew from 14 to 130 people). We had a half-dozen or so Java apps and we found it EXTREMELY valueable to deploy tests into the field to execute them on a specific machine that was exhibiting unusual behavior. Random problems occur in the field and being able to throw a few thousand unit tests at the mystery with zero cost was invaluable and often diagnosed problems in minutes...including installation problems, flaky RAM problems, machine-specific problems, flaky network problems, etc, etc. I think it is incredibly valuable to put tests into the field. Also, random problems pop up at random times and it is nice to have the unit tests sitting there already waiting to be executed at a moments notice. Hard-drive space is cheap. Just like we try to keep data and functions together (OO design), I think there is something fundamentally valuable in keeping code and tests together (function + tests that validate the functions).

I would like to put my tests in the same project in C#/.NET/Visual Studio 2008, but I still haven't investigated this enought to achieve it.

One big benefit of keeping Foo.cs in the same project as FooTest.cs is that developers are constantly reminded when a class is missing a sibling test! This encourages better test-driven coding practices...holes are more apparent.

5
  • 21
    I can see how the would be valuable with integration tests, but for unit tests? If you write them correctly they shouldn't have any dependencies on the machine. Apr 26, 2012 at 18:51
  • +1 I agree. As i am doing mostly .NET, shipping testing with production code also has the nice side effect of reducing the continuous integration step of compile & test: 1) Only half projects to build, 2) all testing assemblies in the place of the main application so it is easier to parameterize your test runner. When using Maven this argument does not hold, of course. Finally, my customers are more technical folks and they really like to be able to run the tests themselves as this is documenting the current state against the spec.
    – mkoertgen
    Oct 4, 2013 at 10:57
  • I think it can make perfect sense to do integration testing in TDD/BDD style, that is writing test code that can be easily automated with standard test runners like NUnit, xUnit etc. Of course, you should not mix up unit with integration testing. Just make sure you know which set of tests to run quickly & often for build verification (BVT) and which ones for integration testing.
    – mkoertgen
    Oct 4, 2013 at 11:02
  • 1
    The reason people object to it is because that's not what they're used to. If their first experience unit testing was that they were within the production code, in fact, with specific syntax in the programming language that indicated what a test is associated with a production method, they would swear that that was an invaluable aspect of the development workflow and it's absolutely crazy to put them in a separate project. Most devs are like this. Followers. Dec 10, 2014 at 0:57
  • You should not force test bits on your customers. Publishing unit test for your components is a best practice however, just don't include them with your production bits. Not everybody needs them and they can cause breakage, when your test infrastructure conflicts with mine.
    – jwdonahue
    Feb 2, 2021 at 22:40
59

After spending some time in TypeScript projects, where tests are often placed in a file alongside the code they are testing, I grew to prefer this approach over keeping them separate:

  • It is quicker to navigate to the test file.
  • It is easier to remember to rename the tests when you rename the class being tested.
  • It is easier to remember to move the tests when you move the class being tested.
  • It is immediately obvious if a class is missing tests.
  • You don't need to manage two duplicate file structures, one for tests and one for code.

So when I started a new .NET Core project recently I wanted to see if it was possible to mimic this structure in a C# project without shipping the tests or test assemblies with the final release.

Putting the following lines in the project file appears to be working well so far:

  <ItemGroup Condition="'$(Configuration)' == 'Release'">
    <Compile Remove="**\*.Tests.cs" />
  </ItemGroup>
  <ItemGroup Condition="'$(Configuration)' != 'Release'">
    <PackageReference Include="nunit" Version="3.11.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
  </ItemGroup>

The above ensures that in the Release configuration all the files named *.Tests.cs are excluded from compilation, and also that the required unit testing package references are removed.

If you still want to be able to unit test the classes in their release configuration you can just create a new configuration derived from Release called something like ReleaseContainingTests.


Update: After using this technique for a while I've also found it's helpful to customize your icons in VS Code to make the tests (and other things) stand out a bit more in the explorer pane:

VS Code Screenshot

To do this, use the Material Icon Theme extension and add something like the following to your VS Code preferences JSON:

"material-icon-theme.files.associations": {
  "*.Tests.cs": "test-jsx",
  "*.Mocks.cs": "merlin",
  "*.Interface.cs": "Raml",
}

Update 2:

If you get the error Program has more than one entry point defined then you can follow the instructions here, which is basically to add <GenerateProgramFile>false</GenerateProgramFile> inside a <PropertyGroup> element in your project's .csproj file, and this will stop the testing framework from adding its own entry point.

12
  • 2
    Exactly what we've just started doing May 28, 2020 at 16:31
  • Have you got this working for .net standard class libraries as well?
    – Zenuka
    Jul 24, 2020 at 11:31
  • 2
    @RoboKozo you can add <GenerateProgramFile>false</GenerateProgramFile> in your project file inside the <PropertyGroup> tag. See here for more: andrewlock.net/… Feb 26, 2021 at 16:18
  • 1
    Great, the trick with the icons did not take effect for me, but otherwise the approach (if that's what you want) works really well. I'd suggest adding the advice about GenerateProgramFile in the answer as well as it bit me. Jul 16, 2021 at 10:41
  • 1
    @JamesThurley it turned out that it's a conceptual limitation, as targeting netstandard does not imply anything on target runtime. However, from net5.0 we can do this again. This aligns well with @orip's answer, because in shared library code netstandard is still preferred for wider compatibility. So to summarize, from net5.0 on, I prefer using the same project in application projects, and separate project for shared library's targetting netstandard. Mar 16, 2022 at 8:17
22

Put Unit tests in the same project as the code to achieve better encapsulation.

You can easily test internal methods, which means you wont make methods public that should have been internal.

Also it's really nice to have the unit tests close to the code you're writing. When you write a method you can easily find the corresponding unit tests because it's in the same project. When you build a assembly that includes unitTests, any errors in the unitTest will give you an compilereerror, so you must keep your unittest up-to-date, just to build. Having unittest in a seperate project, might cause some developers to forget building the unittest-project, and missing the broken tests for a while.

And you can remove the unit tests from the production code, by using compilation tags (IF #Debug).

Automatic Integration Tests (made i NUnit) should be in a seperate project since they don't belong to any single project.

5
  • 1
    But that means you cannot run tests on the Release build and few "heisenbugs" can fall through.
    – hIpPy
    Jun 6, 2013 at 16:37
  • 5
    With friend assemblies ( msdn.microsoft.com/en-us/library/0tke9fxk.aspx ) using InternalsVisibleTo allows you to test internal methods from a seperate test project
    – ParoX
    Mar 26, 2016 at 18:47
  • 3
    @hlpPy - you can configure arbitrary conditional compilation symbols, and you can have more than 2 build configurations. E.g; you might have Debug, Approval and Release; and compile approval with all release optimizations. Also, typically unit tests aren't great at detecting heisenbugs in the first place (in my experience). You tend to need specific integration regression tests for those - and you may well place those side-by-side. Sep 28, 2016 at 13:43
  • A separate project gives the same compile time error. If you need to get that deep in the code then sounds more like you need more abstractions than hack unit tests into your code. Relying conditionals in your code to switch branches is not good either. Sounds more like Unit and Integrations tests are being confused. Oct 28, 2019 at 14:18
  • My main concern with shipping production + test code in an assembly, is that they tend to exponentially increase the attack surface. Online code is subject to many known/unknown vulnerabilities, why would anyone want to increase that surface area? You may not think your component is security critical, but you have no idea how security sensitive the systems are that employ your component. The bigger the pre-positioned tool kit on the server, the easier it is for an attacker to own it, and your test code could easily become the key vector in a successful attack.
    – jwdonahue
    Feb 2, 2021 at 23:03
12

My unit tests always go in a separate project. In fact, for every project I have in my solution, there is a separate test project that goes along with it. Testing code is not application code and should not be intermingled with it. One advantage to keeping them in separate projects -- at least using TestDriven.Net -- is that I can right-click on a test project and run all the tests in that project, testing an entire library of application code with one click.

8
  • 1
    Then how do you unit test internal classes? Or do you only test the public classes?
    – user19371
    Dec 7, 2008 at 3:57
  • 9
    <assembly:InternalsVisibleTo="TestProject" /> if necessary, although I typically only test public interfaces.
    – tvanfosson
    Dec 7, 2008 at 4:21
  • @tvanfosson, what do you do with Specs (Specflow)? Would you have a separate project or would you put them in the Unit Test project? +1.
    – w0051977
    Mar 22, 2018 at 17:03
  • @w0051977 I've never used Specflow so I don't know
    – tvanfosson
    Mar 22, 2018 at 17:31
  • @tvanfosson, what about integration tests? Would you put them in a separate project to the unit tests?
    – w0051977
    Mar 22, 2018 at 17:35
11

If NUnit framework is used, there is an additional reason to put the tests in the same project. Consider the following example of the production code mixed with unit tests:

public static class Ext
{
     [TestCase(1.1, Result = 1)]
     [TestCase(0.9, Result = 1)]
     public static int ToRoundedInt(this double d)
     {
         return (int) Math.Round(d);
     }
}

The unit tests here serve as documentation and specification to the code being tested. I do not know how to achieve this effect of self-documenting, with the tests located in a separate project. The user of the function would have to search for the tests to see those test cases, which is unlikely.

Update: I know that such usage of TestCase attribute was not that the developers of NUnit intented, but why not?

1
  • 2
    I like this idea; having a few examples of inputs and expected outputs right there with with the code. Jun 10, 2019 at 22:23
3

I fluctuate between same project and different projects.

If you're releasing a library releasing the test code with the production code is a problem, otherwise I find it usually isn't (although there's a strong psychological barrier before you try).

When putting tests in the same project I find it easier to switch between tests and the code they test, and easier to refactor/move them around.

3

I put them in separate projects. The name of the assembly mirrors that of the namespaces, as a general rule for us. So if there is a project called Company.Product.Feature.sln, it has an output (assembly name) of Company.Product.Feature.dll. The test project is Company.Product.Feature.Tests.sln, yielding Company.Product.Feature.Tests.dll.

You are best keeping them in a single solution and controlling the output via the Configuration Manager. We have a named configuration for each of the main branches (Development, Integration, Production) in lieu of using the default Debug and Release. Once you have your configurations setup, you can then include or exclude them by clicking on the "Build" checkbox in the Configuration Manager. (To get the Configuration Manager, right-click the solution and go to Configuration Manager.) Note, that I find the CM in Visual Studio to be buggy at times. A couple of times, I have had to go into the project and/or solution files to clean up the targets that it created.

Additionally, if you are using Team Build (and I am sure that other .NET build tools are the same) you can then associate the build with a named configuration. This means that if you don't build your unit tests for your "Production" build, for example, the build project can be aware of this setting as well and not build them since they were marked as such.

Also, we used to do XCopy drops off of the build machine. The script would just omit copying anything named *.Tests.Dll from being deployed. It was simple, but worked.

3

I know this is a very old question, but I would like to add my experience there I recently change unit testing habit from separate projects to same one.

Why?

First I am very tend to keep main project folder structure same with test project. So, if I have a file under Providers > DataProvider > SqlDataProvider.cs then I am creating same structure in my unit test projects like Providers > DataProvider > SqlDataProvider.Tests.cs

But after project is getting bigger and bigger, once you move files around from one folder to another, or from one project to another, then it is getting very cumbersome work to sync those up with unit test projects.

Second, it is not always very easy to navigate from class to be tested to unit test class. This is even harder for JavaScript and Python.

Recently, I started to practice that, every single file I created (for example SqlDataProvider.cs) I am creating another file with Test suffix, like SqlDataProvider.Tests.cs

At the beginning it seems it will bloat up files and library references, but at long term, you will eliminate moving file syndrome at first glance, and also you will make sure, every single file those are candidates of being tested will have a pair file with .Tests suffix. It gives you easy of jumping into test file (because it is side by side) instead of looking through separate project.

You can even write business rules to scan through project and identify class which does not have .Tests file, and report them to the owner. Also you can tell your test runner easily to target .Tests classes.

Especially for Js and Python you will not need to import your references from different path, you can simply use same path of target file being tested.

I am using this practice for a while, and I think it is very reasonable trade-off between project size vs maintainability and learning curve for new comers to the project.

1
  • I disagree. It's much more organized to group items then testing individual classes. If your testing your providers; You would have an IProvider, ISqlProvider, and a MockSqlConnection. In a Provider folder you would have the Unit Test for each provider. You can go that deep on unit tests, but then your just writing tests and not code and may even remove the pro and starts writing code to test. Oct 28, 2019 at 14:24
1

I would say keep them separate.

On top of the other reasons mentioned, having code and tests together skews test coverage numbers. When you report on unit test coverage - reported coverage is higher because the tests are covered when you run unit tests. When you report on integration test coverage, the reported coverage is lower because integration tests would not run unit tests.

1
  • Does that not depend on the technology used to cover the tests? I mean, OpenCover considers lines of code covered if they are run by a test, including the lines of the test itself so that if tests have unexpected exceptions, they are flagged as uncovered too. Apr 7, 2016 at 13:33
0

I am really inspired by the unit testing framework of the Flood NN library by Robert Lopez. It uses a different project for every single unit tested class, and has one solution holding all these projects, as well as a main project that compiles and runs all the tests.

The neat thing is also the layout of the project. The source files are in a folder, but then the folder for the VS project is below. This allows you to make different subfolders for different compilers. All the VS projects are shipped with the code, so it is very easy for anyone to run any or all of the unit tests.

0
0

As others have answered - put tests in separate projects

One thing that hasn't been mentioned is the fact that you can't actually run nunit3-console.exe against anything but .dll files.

If you're planning on running your tests via TeamCity, this will pose a problem.

Let's say you have a console application project. When you compile, it returns an executable .exe to the bin folder.

From the nunit3-console.exe you wouldn't be able to run any tests defined in that console application.

In other words, a console application returns an exe file and a class library returns dll files.

I just got bit by this today and it sucks :(

0

I have recently been deploying in docker. I don't see the value in having a separate project when the Dockerfile can easily copy the /src directory and leave the /test directory. But I am new to dotnet and might be missing something.

0

Another disadvantage of shipping unit test code to production is that we are shipping the knowledge of how to actually use that code. This can be bad because the attacker will have an easier time understanding the business logic after successful decompilation of the unit tests. This can be really harmful. For example, your code could contain special logic to deal with the particularities of taxes between states and countries. This knowledge would then be easily stolen by the attacker, who will have the possibility to sell this to other players in the same area...just by copying the code in production. In my experience, almost no code in .NET is correctly obfuscated in production. There are many tools that can easily decompile the code in a very intelligible form.

2
  • It really depends who is the customer, what if the customer of the code is a developer? what if security isn't a concern at all? and even if security is a concern there are multiple ways to exclude the tests from release builds. You make various assumptions but you don't answer the question.
    – iam3yal
    May 17, 2022 at 21:24
  • If security is not a problem or if there is a good solution like conditional compilation available, I would definitely use the same project to run the tests. Otherwise, the creation of test projects is a necessary evil. If it can be avoided, we should simplify things. In cloud serverless scenarios, I would consider the single-project approach, for example. May 18, 2022 at 10:12
-7

Do you put unit tests in the same project for convenience or do you put them in a separate assembly?

No, never, ever do that. The consumers of your component cannot separate your test code from your production code and therefore, they cannot use it for anything non-trivial.

From the perspective of a security minded consumer, every public symbol in your assembly is part of the attack surface area of your customer's products. Why would they want to use an assembly that includes any non-production security surface area?

SHAME ON YOU IF YOU THINK IT'S OKAY TO INCLUDE UNIT TESTS IN PRODUCTION ASSEMBLIES!

And shame on you if you accept such assemblies for inclusion in your products.


Is it okay to publish your test assemblies? Of course it is, and I consider it to be a best practice. They can be a great aid to customer support, and your customers might also want to incorporate them into their own test environments. Just don't force your test bits on your customers.

4
  • So many down votes and nobody willing to say why.
    – jwdonahue
    Feb 3, 2022 at 18:14
  • 1
    I would guess the downvotes are not for your opinion but more how you expressed it by using the word shame. It was an instant turn off for me reading it. Feb 19, 2022 at 20:48
  • @CraigNorton Well the cost of gross negligence in the software development community exceeds a trillion dollars per year at this point, so ya, anybody that thinks it's anything less than shameful to ignore common sense security practices, deservers to be made to feel uncomfortable. But I suppose I could reword it. Any suggestions that won't water it down to the point of irrelevance?
    – jwdonahue
    Feb 24, 2022 at 20:29
  • @CraigNorton now I think of it. It isn't necessarily harmful if the test code is in there for test builds, provided they are not compiled into the released product, but the unit complexity goes up exponentially (generally more test than product code). Tooling could solve that.
    – jwdonahue
    Feb 24, 2022 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.