Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

[Fixes #6233] Productionize and harden our functional testing infrastructure #6433

Merged
merged 6 commits into from
Jun 24, 2017

Conversation

javiercn
Copy link
Member

Adds APIs for bootstrapping an application in memory.
Integration with our current functional test infrastructure is pending.
Open questions:

  • Should we plug-in the cookie container handler by default?

@@ -0,0 +1,38 @@
using System.Net;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

license header

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(AspNetCoreVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop Microsoft.AspNetCore.Testing and xunit from here? I know we will get feedback about bundling xunit with this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with the bundled approach as its what we talked about for simplicity. We can definitely make it two packages
Microsoft.AspNetCore.Mvc.Testing and Microsoft.AspNetCore.Mvc.Testing.Xunit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that there's any need for an xunit specific package. The most valuable piece of this is the test fixture and that's not xunit specific.


<ItemGroup>
<Content Include="build\**\*.targets" Pack="true" PackagePath="%(Identity)" />
<Content Include="build\xunit.runner.json" Pack="true" PackagePath="%(Identity)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should be overriding users xunit.runner.json file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only includes it in the package. That said, what we can do is look in the output folder if an xunit.runner.json file exists and only if it doesn't, we can copy ours to the output.

}
while (directoryInfo.Parent != null);

throw new Exception($"Solution root could not be located using application root {applicationBasePath}.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice


builder.UseStartup<TestStartup<TStartup>>();

return new TestServer(builder);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have these verify that the .deps file is present (and anything else required). This is the most common issue that our users run into.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add checks based on whether the user called UseApplicationAssemblies and maybe we can do something to verify the xunit configuration.

There are a few ways of configuring XUnit, not only xunit.runner.json so we can't really tell for sure if shadowCopy (what we care about) is disabled.

Are you ok if we copy xunit.runner.json by default (if the file does not exist on the output) and we provide a property on MSBuild to disable it? We can add a validation in code that checks for xunit.runner.json on the current directory and prints to the console in case its not there or doesn't contain the values that we need. (We can disable the logging to with an environment variable, for the case you go "i know what i'm doing" all the way)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really prefer that this package doesn't have anything related xunit anything in it. I think it's just not needed, and if we stick xunit stuff in here users of other testing frameworks going to want it taken out.

Follow my reasoning for a set.

  1. We have an msbuild targets file that copies the deps file
  2. Make it an msbuild error if your 'app' project doesn't have a deps file
  3. Then at runtime if you don't have a deps file the only possibility is that you're shadow copied. This should throw an exception and tell you to turn off shadow copying.

/// A middleware that ensures web sites run in a consistent culture. Currently useful for tests that format dates,
/// times, or numbers. Will be more useful when we have localized resources.
/// </summary>
public class CultureReplacerMiddleware
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to pull a bunch of this stuff out of the Mvc.Testing namespace into something more obscure. You really only need a few of these classes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, pretty much everything but WebApplicationBuilder can be on the .Internal namespace

/// XUnit fixture for bootstrapping an application in memory for functional end to end tests.
/// </summary>
/// <typeparam name="TStartup">The applications startup class.</typeparam>
public class WebApplicationTestFixture<TStartup> : IDisposable where TStartup : class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice that we can ship this without any direct xunit dependency.

@@ -0,0 +1,41 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use this in our functional test? So we test the tester?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do but through a direct import, due to the fact that target files are only automatically imported for package references, we'll have some E2E tests for this on the identity repo.

assembly dependencies in websites to their corresponding bin/{config}/refs folder we need to re-calculate
reference assemblies for this project so there's a corresponding refs folder in our output. Without it
our websites deps files will fail to find their assembly referenes.
-->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just about our stuff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that, I just copied all the custom stuff from the functional test project into the targets file. @pranavkm any idea what this comment is for?

Copy link
Member

@rynowak rynowak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great start. Feel free to do an initial check in and then do follow ups for issues, or resolve stuff now if you'd prefer that.

services.AddTransient<HtmlEncoder, HtmlTestEncoder>();
services.AddTransient<JavaScriptEncoder, JavaScriptTestEncoder>();
services.AddTransient<UrlEncoder, UrlTestEncoder>();
services.TryAddTransient<HtmlEncoder, HtmlTestEncoder>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we want to force add these (and not TryAdd)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why they are added before startup configure services run. It tests that even if you do it this way it will work.

@javiercn javiercn merged commit 5b6d73e into dev Jun 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants