Reputation: 75
I am using CSV files as datasource for my MSTest unit tests. I had the idea that I would generate dynamic file paths for the CSV file, store them in a variable and then pass the variable in ConnectionString of databasesource for the unit tests.
However, now I have learned that we cannot pass variable in Datasource Connection string. Any idea how can I make the file path dynamic as the unit test dll will be executed on different machines and a static path is not an option.
The CSV files are already added in the solution.
Upvotes: 0
Views: 615
Reputation: 257
public string GetAbsolutePath(string relativePath)
{
string path = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
int lastIndex = path.LastIndexOf("bin", StringComparison.OrdinalIgnoreCase);
string actualPath = path.Substring(0, lastIndex);
string projectPath = new Uri(actualPath).LocalPath;
string absolutePath = projectPath + relativePath;
return absolutePath;
}
The above code is for c# will run on any machine to reach file inside your project. this code will give your project folder path in "projectPath " variable even if you use different machines because it is generated with reference to your dll file. the only thing you need to do is pass relative path of folder or file you want to reach inside your project.
Note:- find bin folder in your project "projectPath" variable will give you path till that and then accordingly pass your relative path as a parameter. Please debug function to get a clear understanding.
Let me know if you need clarification
Upvotes: 0