Here is my unit test code:
[TestMethod]
public void GetMyAttachmentTest()
{
var files = Directory.GetFiles(
Directory.GetCurrentDirectory() + "/Common/MyFiles/", "*.*");
.... do some thing with the files...
}
On TFS build machine, when I run my unit test, I am getting the following error:
System.IO.DirectoryNotFoundException: Could not find a part of the path
'C:\Builds\4\30 my folder v1.0\myfolder1\TestResults\SCMTFSService_MyBuildServer 2013-01-16 18_06_52_Any CPU_Release\Out\Common\MySpecialFiles\'.
When I look into the drop folder in TFS, I see that my files are deployed into the following folder:
\\MyBuildServer\Builds\MyFolder\MySolution_20130116.5\Common\MySpecialFiles
Inside my unit test code, I get my files as follows:
var files = Directory.GetFiles(
Directory.GetCurrentDirectory() + "/Common/MySpecialFiles/", "*.*");
Is there a way when my unit test runs on TFS build machine that it can look into the files in the deployed Common\MySpecialFiles?
The question is a bit vague. In the past, I've had issues with unit tests executing in temp locations. This can be problematic with dynamic paths such as GetCurrentDirectory(). When the the temp directory changes/clears, files can be "lost".
Instead of using a dynamic path, setup a network share with appropriate permissions. I recommend storing the path to this network share a config file or the DB so that it can be updated without a code push.
Why not put the location of the files in an appSettings item in the app.config/web.config file? That will make it easy to change it to match your environment whether it's local, a build server or a deployed environment.
Otherwise, you will have to set the current directory to \MyBuildServer\Builds\MyFolder\MySolution_20130116.5 before running your test and that will probably impact your tests in other ways.
Related
I have some Selenium C# tests hosted on Azure which they need to look for the pre-built excel file in project tree, stored inside bin folder, to execute some file upload validation scenarios.
When they are executed locally these scenarios pass without any problem, but when it comes to be executed on the Azure they receive the following error.
invalid argument: File not found : D:\a\r1\a_Selenium_Tests\TestApplication\bin\Debug\netcoreapp3.1\Files\SC003_CT014_ActiveEmployees.xlsx
The files do exists in the following path: ...\bin\Debug\netcoreapp3.1\Files...
And the code I use to them is:
string root = Directory.GetCurrentDirectory() + "\\Files\\" + file;
Do you know if there's a missing file configuration or building the filePath in another way?
Thanks for your help :D
Directory.GetCurrentDirection() returns the current working directory, not the folder in which the DLL file resides. The current working directory is a different thing. In your case, the current working directory is probably something like D:\a\r1\. Instead, you need to get the folder in which the test assembly resides:
var binDirectory = Path.GetDirectoryName(GetType().Assembly.Location);
// ^^^^^^^^^
var excelFilePath = Path.Combine(binDirectory, "Files", "SC003_CT014_ActiveEmployees.xlsx");
Note: Replace GetType() with typeof(ClassName) if you are executing your code from a static method, or you would like to specify a path to a different assembly than the one that is currently executing.
So I have these lines of code right here-
// on a network location
string fileName = #"S:\Information Technology\QA\Automation\jsons\scheduler_ready\patientInfo.json";
string json = TestHelper.GetJsonFromFilePath(fileName);
patient = TestHelper.ExtractJsonData(json);
I have a NUnit test that relies on these getting information from a JSON. The issue I'm having is the execution. When I run through Visual Studio, I can read the filepath just fine. But when I execute through NUnit-Console runner, I get an error that says "part of file path not found" for the json.
I researched and so far no luck. Has anyone come into contact with this problem?
Do I need to take extra steps to tell the console that it is my user that is accessing this file location?
EDIT
I changed my file path to a local location. It seems I can't access a network drive through the nunit-console runner. Is there a way to accomplish this?
Is the json file part of the solution directory? If it is, you can dynamically format it:
string fileName = $#"{folderPath}\Automation\jsons\scheduler_ready\patientInfo.json";
string json = TestHelper.GetJsonFromFilePath(fileName);
patient = TestHelper.ExtractJsonData(json);
You can get the folder path with one of these values:
System.Reflection.Assembly.GetEntryAssembly().Location;
AppDomain.CurrentDomain.BaseDirectory
Assembly.GetEntryAssembly().Location
We are using the new DbMigration.SqlFile method in EF Migrations 6.1.2 to run a migration script in our migration. According to the documentation, the file has to be relative to the current AppDomain BaseDirectory. We have included these files in the project, and set them to copy to output directory.
Locally this all runs fine. They get output to the bin directory, and run fine.
When deploying the software to a server running IIS however, the migration fails, because it suddenly expects the files to be relative to the root. When I copy them there, the migration works.
How can I use DbMigration.SqlFile so it runs correctly both locally and on the server?
The SqlFile method uses the CurrentDomain.BaseDirectory if a relative path is given. A workaround is to map the path yourself and give an absolute path to the method. A solution would look like this:
var sqlFile = "MigrationScripts/script1.sql";
var filePath = Path.Combine(GetBasePath(), sqlFile);
SqlFile(filePath);
public static string GetBasePath()
{
if(System.Web.HttpContext.Current == null) return AppDomain.CurrentDomain.BaseDirectory;
else return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"bin");
}
BasePath solution taken from: Why AppDomain.CurrentDomain.BaseDirectory not contains "bin" in asp.net app?
We're using it like this from within the migration: SqlFile(#"..\..\Sql\views\SomeView.sql");
I will try to make it clear
I have a MSTest Project called IntegrationTests
I have a PowerShell script inside IntegrationTests folder. This script runs the test using MSTest command line arguments.
The tests are being successfully called but after all the tests run there is a call to one method in my test method that creates report. When its trying to load the report.xslt file it is adding extra folder "TestResults"
Unexpected
................
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\dev\Desktop\Test Runner\IntegrationTests\TestResults\Report Generator\Reports\report.xslt
Expected
................
C:\Users\dev\Desktop\Test Runner\IntegrationTests\Report Generator\Reports\report.xslt
why is this "TestResults" extra folder is added?
Just to make things clear this project is 100% working when i run this from visual studio.
If you would like to know how the relative path is being constructed in c# here is the code
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var xslt = new XslCompiledTransform();
xslt.Load(Path.Combine(directory, "..\\..\\Report Generator\\Reports\\report.xslt"));
I just want to know how can i get rid of "TestResults" so that my test can run normally. Any helps really appreciated.
I have a unit test project set up in the same solution as my project in Visual Studio. Unit testing is being done via built in Unit Testing tools in Visual Studio (included in Premium and above versions). I need to load a file that is in the path of the project itself, not the test project, while running unit tests in the test project.
The file to include is part of the main project, and has the following properties:
Build Action: Content
Copy to Output Directory: Always
I need to write a unit test that for a function that depends on this file, or I will hit an error state and will not be able to write tests for 100% coverage on that function.
How would I get the execution path of the actual project from the unit test project?
Edit: The specific function reads all lines in the config file and stores them one at a time in a list. Sample code follows:
public List<string> LoadConfigFile() {
List<string> models = new List<string>();
StreamReader sr = new StreamReader(Path.GetDirectoryName(Application.ExecutablePath) + #"\" + Properties.Resources.SupportedModelsConfigFile);
while ((line = sr.ReadLine()) != null)
{
models.Add(line);
}
sr.Close();
return models;
}
Primary Problem: Application.ExecutablePath works fine when running the program inside or outside of the IDE, but when running unit tests, it sends me to a directory within visual studio, specifically this directory:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32.exe
you could set a variable to get the path of where the application is being launched from
var execPath = AppDomain.CurrentDomain.BaseDirectory;