3

I'm integration testing an ASP.NET Core app by using WebApplicationFactory. When starting the test server, I want to pass a command-line argument to the tested app, so to the Main method contained in the assembly to which TEntryPoint belongs. How to do that?

I tried using the nuget package Microsoft.Extensions.Configuration.CommandLine, but obviously that's for another purpose than what I want, as I don't want to configure the tested app differently.

1
  • 1
    After analyzing a little, i found suggestion that you could try override void ConfigureWebHost in WebApplicationFactory<Program>, and this way you could inject some overrides in configuration, then use it in web app - you would use CLI arguments, optionally overriding them with the config. This SO post might be of interest Commented Aug 31 at 6:17

1 Answer 1

1

Assuming you only want to extend/change the configuration with command line args, you can override the configuration setup and pass down your arguments, like

using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.Configuration; using System.Collections.Generic; public class CustomWebApplicationFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class { private readonly string[] _commandLineArgs; public CustomWebApplicationFactory(string[] commandLineArgs) { _commandLineArgs = commandLineArgs; } protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureAppConfiguration((context, configBuilder) => { configBuilder.AddCommandLine(_commandLineArgs); }); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.