Make functional tests run for desktop

* Convert functional tests to run using TestServer
* Allow functional tests to run in parallel.

Fixes #360
Fixes #390
This commit is contained in:
Pranav K 2017-08-08 15:33:14 -07:00
parent fe3b5ea4dd
commit a403d39405
6 changed files with 59 additions and 158 deletions

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Import Project="..\..\build\dependencies.props" />
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>

View File

@ -1,27 +1,38 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using LocalizationSample;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
namespace Microsoft.AspNetCore.Localization.FunctionalTests
{
public class LocalizationSampleTest
{
private static readonly string _applicationPath = Path.Combine("samples", "LocalizationSample");
[Fact]
public Task RunSite()
public async Task LocalizationSampleSmokeTest()
{
var testRunner = new TestRunner(_applicationPath);
// Arrange
var webHostBuilder = new WebHostBuilder().UseStartup(typeof(Startup));
var testHost = new TestServer(webHostBuilder);
var locale = "fr-FR";
var client = testHost.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "My/Resources");
var cookieValue = $"c={locale}|uic={locale}";
request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}");
return testRunner.RunTestAndVerifyResponseHeading(
RuntimeArchitecture.x64,
"My/Resources",
"fr-FR",
"<h1>Bonjour</h1>");
// Act
var response = await client.SendAsync(request);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("<h1>Bonjour</h1>", await response.Content.ReadAsStringAsync());
}
}
}

View File

@ -1,25 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using LocalizationWebsite;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
namespace Microsoft.AspNetCore.Localization.FunctionalTests
{
public class LocalizationTest
{
private static readonly string _applicationPath = Path.Combine("test", "LocalizationWebsite");
[Fact]
public Task Localization_CustomCulture()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"CustomCulturePreserved",
return RunTest(
typeof(StartupCustomCulturePreserved),
"en-US",
"kr10.00");
}
@ -27,11 +26,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
[Fact]
public Task Localization_ResourcesInClassLibrary_ReturnLocalizedValue()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"ResourcesInClassLibrary",
return RunTest(
typeof(StartupResourcesInClassLibrary),
"fr-FR",
"Bonjour from ResourcesClassLibraryNoAttribute Bonjour from ResourcesClassLibraryNoAttribute Bonjour from ResourcesClassLibraryWithAttribute Bonjour from ResourcesClassLibraryWithAttribute");
}
@ -39,11 +35,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
[Fact]
public Task Localization_ResourcesInFolder_ReturnLocalizedValue()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"ResourcesInFolder",
return RunTest(
typeof(StartupResourcesInFolder),
"fr-FR",
"Bonjour from StartupResourcesInFolder Bonjour from Test in resources folder Bonjour from Customer in resources folder Hello");
}
@ -51,11 +44,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
[Fact]
public Task Localization_ResourcesInFolder_ReturnLocalizedValue_WithCultureFallback()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"ResourcesInFolder",
return RunTest(
typeof(StartupResourcesInFolder),
"fr-FR-test",
"Bonjour from StartupResourcesInFolder Bonjour from Test in resources folder Bonjour from Customer in resources folder Hello");
}
@ -63,11 +53,8 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
[Fact]
public Task Localization_ResourcesInFolder_ReturnNonLocalizedValue_CultureHierarchyTooDeep()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"ResourcesInFolder",
return RunTest(
typeof(StartupResourcesInFolder),
"fr-FR-test-again-too-deep-to-work",
"Hello Hello Hello Hello");
}
@ -75,13 +62,26 @@ namespace Microsoft.AspNetCore.Localization.FunctionalTests
[Fact]
public Task Localization_ResourcesAtRootFolder_ReturnLocalizedValue()
{
var testRunner = new TestRunner(_applicationPath);
return testRunner.RunTestAndVerifyResponse(
RuntimeArchitecture.x64,
"ResourcesAtRootFolder",
return RunTest(
typeof(StartupResourcesAtRootFolder),
"fr-FR",
"Bonjour from StartupResourcesAtRootFolder Bonjour from Test in root folder Bonjour from Customer in Models folder");
}
private async Task RunTest(Type startupType, string culture, string expected)
{
var webHostBuilder = new WebHostBuilder().UseStartup(startupType);
var testHost = new TestServer(webHostBuilder);
var client = testHost.CreateClient();
var request = new HttpRequestMessage();
var cookieValue = $"c={culture}|uic={culture}";
request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}");
var response = await client.SendAsync(request);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains(expected, await response.Content.ReadAsStringAsync());
}
}
}

View File

@ -8,11 +8,13 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\samples\LocalizationSample\LocalizationSample.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Localization\Microsoft.AspNetCore.Localization.csproj" />
<ProjectReference Include="..\LocalizationWebsite\LocalizationWebsite.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.IntegrationTesting" Version="$(AspNetIntegrationTestingVersion)" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="$(AspNetCoreVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />

View File

@ -1,3 +0,0 @@
using Xunit;
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]

View File

@ -1,109 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.Logging;
using Xunit;
namespace Microsoft.AspNetCore.Localization.FunctionalTests
{
public class TestRunner
{
private const string ApplicationBasePath = "http://localhost:0";
private string _applicationPath;
public TestRunner(string applicationPath)
{
_applicationPath = Path.Combine(ResolveRootFolder(AppContext.BaseDirectory), applicationPath);
}
private static string ResolveRootFolder(string projectFolder)
{
var di = new DirectoryInfo(projectFolder);
while (di.Parent != null)
{
var globalJsonPath = Path.Combine(di.FullName, "version.xml");
if (File.Exists(globalJsonPath))
{
return di.FullName;
}
di = di.Parent;
}
// If we don't find any files then make the project folder the root
return projectFolder;
}
private async Task<string> RunTestAndGetResponse(
RuntimeArchitecture runtimeArchitecture,
string environmentName,
string locale)
{
var loggerFactory = new LoggerFactory();
var deploymentParameters = new DeploymentParameters(_applicationPath, ServerType.Kestrel, RuntimeFlavor.CoreClr, runtimeArchitecture)
{
ApplicationBaseUriHint = ApplicationBasePath,
EnvironmentName = environmentName,
TargetFramework = "netcoreapp2.0"
};
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var cookie = new Cookie(CookieRequestCultureProvider.DefaultCookieName, "c=" + locale + "|uic=" + locale);
var cookieContainer = new CookieContainer();
cookieContainer.Add(new Uri(deploymentResult.ApplicationBaseUri), cookie);
var httpClientHandler = new HttpClientHandler();
httpClientHandler.CookieContainer = cookieContainer;
using (var httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) })
{
var logger = loggerFactory.CreateLogger(string.Format("Localization Test Site:{0}:{1}", ServerType.Kestrel, runtimeArchitecture));
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
var response = await RetryHelper.RetryRequest(() =>
{
return httpClient.GetAsync(string.Empty);
}, logger, deploymentResult.HostShutdownToken);
return await response.Content.ReadAsStringAsync();
}
}
}
public async Task RunTestAndVerifyResponse(
RuntimeArchitecture runtimeArchitecture,
string environmentName,
string locale,
string expectedText)
{
var responseText = await RunTestAndGetResponse(runtimeArchitecture, environmentName, locale);
Console.WriteLine("Response Text " + responseText);
Assert.Equal(expectedText, responseText);
}
public async Task RunTestAndVerifyResponseHeading(
RuntimeArchitecture runtimeArchitecture,
string environmentName,
string locale,
string expectedHeadingText)
{
var responseText = await RunTestAndGetResponse(runtimeArchitecture, environmentName, locale);
var headingIndex = responseText.IndexOf(expectedHeadingText);
Console.WriteLine("Response Header " + responseText);
Assert.True(headingIndex >= 0);
}
}
}