How do I start kestrel from a test?

2019-09-09 21:43发布

问题:

I'm setting up regression testing for my ASP.NET 5 project using beta8. When I setup the test fixtures I want to fire up kestrel so that I could run selenium tests against it without the need for any external web server. How do I do this?

It's basically something like this:

public class RegressionTests : IDisposable
{

    public RegressionTests()
    {
        // Start kestrel
    }

    [Fact]
    public void Test1()
    {
        Assert.True(true);
        // more tests...
    }

    public void Dispose()
    {
        // Shutdown kestrel
    }
}

This is what I've tried so far but I couldn't get it to work. It doesn't pick up the project.json file. Well, to be honest, I don't know what to pass to it since I can't find anywhere what command args I can pass to Microsoft.AspNet.Hosting.Program.

new Microsoft.AspNet.Hosting.Program(CallContextServiceLocator.Locator.ServiceProvider).Main(
            new[] 
            {
                "--server",
                "Microsoft.AspNet.Server.Kestrel",
                "--project",
                "../Web/project.json",
                "--port",
                "5001",
            });

回答1:

Take a look at the MusicStore sample E2E Tests. They launch all servers (IIS, Kestrel, WebListener).

Under the hood, they use the deployer in Microsoft.AspNet.Server.Testing.



回答2:

Thanks @Victor Hurdugaci. For the google folks of the future, this is what I ended up having. This is a test fixture that I use for xunit. The TestConfiguration class is missing but you should get the idea. You need to add a dependency on Microsoft.AspNet.Server.Testing.

public class WebTestsFixture : IDisposable
{
    private readonly IApplicationDeployer _deployer;
    private readonly IDisposable _loggerScope;

    public WebTestsFixture()
    {
        var logger = new LoggerFactory()
            .AddConsole(LogLevel.Information)
            .CreateLogger("Regression");

        _loggerScope = logger.BeginScope("RegressionTestSuite");

        var deploymentParameters = new DeploymentParameters(
            TestConfiguration.Configuration.Get<string>("Settings:ApplicationPath"),
            (ServerType)Enum.Parse(typeof(ServerType), TestConfiguration.Configuration.Get<string>("Settings:ServerType")),
            RuntimeFlavor.Clr,
            RuntimeArchitecture.x86)
        {
            ApplicationBaseUriHint = TestConfiguration.Configuration.Get<string>("Settings:ApplicationUri"),
            EnvironmentName = TestConfiguration.Configuration.Get<string>("Settings:EnvironmentName"),
            PublishWithNoSource = false
        };

        _deployer = ApplicationDeployerFactory.Create(deploymentParameters, logger);

        DeploymentResult = _deployer.Deploy();
    }

    public DeploymentResult DeploymentResult { get; private set; }

    public void Dispose()
    {
        _loggerScope.Dispose();
        _deployer.Dispose();
    }
}


回答3:

@mardoxx points out that a more modern and much simpler approach to testing is documented here.