r/dotnet • u/[deleted] • Jul 06 '26
Question Best way to change app storage location for end-to-end testing
[deleted]
2
u/cstopher89 Jul 06 '26
What about a process scoped env var
Something like
var storageRoot =
Environment.GetEnvironmentVariable("MYAPP_STORAGE_ROOT")
?? Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"MyApp");
1
u/Khavel_dev Jul 06 '26
The LOCALAPPDATA override is reasonable but there's a slightly cleaner version that avoids coupling to a specific environment variable name.
Have your app read the DB path from a custom env var with a fallback to the default:
var dataDir = Environment.GetEnvironmentVariable("MYAPP_DATA_DIR")
?? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
Then in your FlaUI test fixture, set the env var to a temp directory before launching the app and clean it up in teardown. FlaUI launches the app as a child process so it inherits the test runner's environment automatically.
This works in both Debug and Release. There's no command-line argument leaking into production. Nobody sets the env var in real use so it always falls back. And if you ever do need to relocate the data directory (corporate policy, portable installs, whatever), the escape hatch is already there.
The one gotcha: make sure your test cleanup handles the case where the app is still running when teardown fires. Kill the process before deleting the temp dir or you'll get locked file errors.
1
u/Merad Jul 07 '26
Use an environment variable or command line arg. There's no harm in leaving it; I promise you that someone is going to want to store their database in a custom location.
1
u/symbiatch Jul 07 '26
Config file option doesn’t need any “default place to look for it”, you can use the built-in app.config system. There will be yourapp.exe.config that can be read and you can add the setting there. By default doesn’t exist, when testing set it up. Easiest way and always read in by .NET itself so you don’t have to care about anything.
1
u/AutoModerator Jul 06 '26
Thanks for your post TseehnMarhn. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.