[{"data":1,"prerenderedAt":130},["ShallowReactive",2],{"/2026/07/22-webapplicationfactory-and-runsettings":3},{"id":4,"title":5,"body":6,"date":121,"description":53,"extension":122,"meta":123,"navigation":124,"path":125,"robots":126,"seo":127,"stem":128,"__hash__":129},"posts/2026/07/22 webapplicationfactory-and-runsettings.md","22 Webapplicationfactory And Runsettings",{"type":7,"value":8,"toc":118},"minimark",[9,18,21,25,27,30,32,35,37,40,54,56,70,72,75,77,80,82,85,93,95,98,106,108],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"webapplicationfactory-and-runsettings","WebApplicationFactory and Runsettings",[19,20],"br",{},[22,23,24],"p",{},"On the project that I'm working on, I intended to have an integration test. But I never built one before and wondering what's the best way to do it for .NET.",[19,26],{},[22,28,29],{},"To do integration test, the idea is we need to deploy the application and write an automation and run it against the deployed application.However, in my case, since the project is a library project, it needs an application that references it. And I don't want to have two additional projects.",[19,31],{},[22,33,34],{},"So, a quick search introduced me to WebApplicationFactory which I can use in my test project. First, I created a regular Program.cs that defines all the pre-launch configurations and services, so I can use dependency injection in the tests.",[19,36],{},[22,38,39],{},"Next, I used WebApplicationFactory to launch the Program for each test case.",[41,42,43],"code-block",{},[44,45,50],"pre",{"className":46,"code":48,"language":49},[47],"language-text","[TestInitialize]\npublic void Setup()\n{\n  var factory = WebApplicationFactory\u003CProgram>();\n\n  var service = factory.ServiceProvider.GetRequiredSerice\u003CISomeService>();\n}\n","text",[51,52,48],"code",{"__ignoreMap":53},"",[19,55],{},[22,57,58,59],{},"To learn more on integration test for .NET: ",[60,61,64],"span",{"className":62},[63],"text-blue-600",[65,66,67],"a",{"href":67,"rel":68},"https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-10.0&pivots=xunit",[69],"nofollow",[19,71],{},[22,73,74],{},"After that, I'm wondering how I can have an environment variable to hold some sensitive information since there's no launchSettings that comes with the project. I might be able to use appSettings.json and I don't want to set it on my machine's environment variable.",[19,76],{},[22,78,79],{},"That's when I found runsettings which allows me to add environment variables for tests. I also added it to .gitignore to prevent sensitive information being checked in into the repository.",[19,81],{},[22,83,84],{},"So, I created .runsettings file and placed it on the root of the solution.",[41,86,87],{},[44,88,91],{"className":89,"code":90,"language":49},[47],"\u003C?xml version=\"1.0\" encoding=\"utf-8\"?>\n\u003CRunSettings>\n  \u003CRunConfiguration>\n    \u003CEnvironmentVariables>\n      \u003CASPNETCORE_ENVIRONMENT>Development\u003C/ASPNETCORE_ENVIRONMENT>\n    \u003C/EnvironmentVariables>\n  \u003C/RunConfiguration>\n\u003C/RunSettings>\n",[51,92,90],{"__ignoreMap":53},[19,94],{},[22,96,97],{},"With .runsettings file on the root of the solution, running test through Visual Studio will automatically detect it and use it. To use runsettings when running tests from command line, it needs to be specified using --settings option.",[41,99,100],{},[44,101,104],{"className":102,"code":103,"language":49},[47],"dotnet test --settings .runsettings\n",[51,105,103],{"__ignoreMap":53},[19,107],{},[22,109,110,111],{},"For more information on runsettings: ",[60,112,114],{"className":113},[63],[65,115,116],{"href":116,"rel":117},"https://learn.microsoft.com/en-us/visualstudio/test/configure-unit-tests-by-using-a-dot-runsettings-file?view=visualstudio",[69],{"title":53,"searchDepth":119,"depth":119,"links":120},2,[],"2026-07-22T00:00:00.000Z","md",{},true,"/2026/07/22-webapplicationfactory-and-runsettings",null,{"title":5,"description":53},"2026/07/22 webapplicationfactory-and-runsettings","aq9NJNbeSNTyJDc6zoXsLpGIqOjzRNsAcQn3iJvVY6Q",1785167451284]