How to get path of project from test project? - c#

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;

Related

Visual Studio 2019 EnvDTE.DTE (C#): Creating new project in current solution by cloning existing project fails

I am working on creating a testing pipeline which automatically pasts a piece of code into a newly created Unit Test Project so that a user can conveniently run Microsoft's auto-test generation tool (IntelliTest) on it. I am working on developing the auto Unit Test Project generation component, and in my existing Visual Studio solution, I have manually created a new Unit Test Project with the exact specifications of what I want (UnitTestTemplate).
I then perform the following steps:
Retrieve the current Visual Studio solution.
Add a new project based upon the UnitTestTemplate csproj file, by using the AddFromFile method (https://learn.microsoft.com/en-us/dotnet/api/envdte._solution.addfromfile?view=visualstudiosdk-2019) on the solution.
Below is my code:
EnvDTE.DTE dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
EnvDTE._Solution solution = dte.Solution;
solution.AddFromFile("C:/NewestCode/TestingAutomation/src/UnitTestGeneration/UnitTestTemplate/UnitTestTemplate.csproj");
However, at Step 2, which is at this line: solution.AddFromFile("C:/NewestCode/TestingAutomation/src/UnitTestGeneration/UnitTestTemplate/UnitTestTemplate.csproj");,
I get the following error:
System.Runtime.InteropServices.COMException: Unspecified error
(Exception from HRESULT: 0x80004005 (E_FAIL))
This seems strange, considering that I know my file path for the .csproj file is correct.

MsTest Fails in CMD when using relative path

I have a folder that contains all the files needed by tests.
testResources
testRes.txt
In my test I have a relativ path to testResources defined in a const
private const string testRes = #"..\..\..\testResources\testRes.txt";
The test that depends on testResources passes, when using visual studio, however when the same test is run by MsTest in command line the tests FAILS, casting file not found exception.
How is that possible and how to fix this`?
More Info:
The output path is set to: bin\Debug\
MsTest running path differ from Visual studio path.
I dont have control over MsTest path.
language: C# .net 4.0

while calling mstest from powershell, extra directory is being added in the relative paths in c#

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.

Using VSTest to run unit test cases instead of MSTest

I have an x64 platform C# solution(VS2012) on a TFS2010 server. I have attached a unit test project (also x64) to this solution and created a build definition. When I queue the build, it succeeds but the unit test cases will not be executed. This is because MSTest is a 32 bit application. So, I decided to customize the default build process template (DefaultTemplate.xaml) to invoke VSTest(VSTest.console.exe) instead of MSTest. This is quite complex and I am unable to add a build activity to the toolbox for VSTest.
Has anyone done this kind of customization? I have also considered other approaches like configuring .runsettings file. Do we have a VSTest adapter interface that can be added in the .runsettings file ?
Executing unit tests through VSTest and publishing the test results through MSTest gave me a successful outcome. Given below is the Powershell script:
# Get the UnitTest Binaries
$files = Get-ChildItem $TestAssembliesDir\*est*.dll
# VSTest.console.exe path
$VSTestPath = 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe'
# MSTest path
$MSTestpath = "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe"
# Loop through the test assemblies and add them to the VSTestFiles
$VSTestFiles = ''
foreach($file in $files)
{
$VSTestFiles += "`"$file`""
$VSTestFiles += " "
}
# Run the UnitTests using VSTest
&$VSTestPath $vstestplatform "/Framework:Framework45" "/InIsolation" $VSTestFiles "/logger:trx"
# Get TRX files
$TrxFilesList = Get-ChildItem $TestResDir\*.trx
$TrxFiles = ''
$loop = 1
foreach($file in $TrxFilesList)
{
$TrxFiles = "$file"
# copy the trx file into a space-less named trx
$newTrxFileName = "$BuildUri" + "_" + "$loop" + ".trx"
copy-item $TrxFiles -destination $TestResDir\$newTrxFileName
$loop = $loop + 1
$newTrxFile += "$TestResDir\$newTrxFileName"
$newTrxFile += " "
}
# specify MSTest arguments
$mspubl = "/publish:"+$TeamProjColUri
$msteampr = "/teamproject:" + $TeamProj
$mspublbuild = "/publishbuild:" +$BuildUri
$mspubresfile = "/publishresultsfile:" +"`"$newTrxFile`""
#Publish test results through MSTest
&$MSTestpath $mstestplatform $flavor $mspubl $msteampr $mspublbuild $mspubresfile
I too have the exact same need for using VSTest.Console.exe instead of MSTest.exe for a TFS2010 build process that compiles a VS2012/.NET 4.5 x64 application, while waiting for the upgrade to TFS2012 to commence.
The approach I have taken was to edit the build script XAML, deleted the existing workflow for unit tests and replaced it with a customised workflow that builds up the VSTest.Console.exe parameters and then executes VSTest.Console.exe via InvokeProcess. I then ensured that in the Finally block that regardless of test result that we publish the test results and code coverage to TFS using MSTest.exe from a VS2012 installation on the build server.
Unfortunately I cannot post the XAML in the answer as it exceeds character length, but I do have a text file consisting of the snippet to be replaced in DefaultTemplate.xaml and what to replace it with. The file can be found here. Please note that although this approach works it is a hack.
Another alternative would be to use NUnit instead of MSTest or VSTest.Console as this support 64-bit binaries. This article explains how to integrate NUnit in a TFS2010 build script, and has links to tools and resources required to make this happen. The only issues with NUnit are code coverage (need yet another tool plus work out how to publish these results to TFS) and MSTest-style integration tests using attributes such as DeploymentItem and properties such as TestContext, which is why where I work we opted with the VSTest.Console.exe approach.
And from what I have read TFS2012 offers easy integration to VSTest.Console.exe from build scripts, so if you do ever upgrade to TFS2012 the VSTest.Console.exe hack that I have documented may not be required.
This does not directly answer you question, but it might help. I did a similar thing for TeamCity. I used command-line to call vstest.console.exe and created a .runsettings file.
I used this Microsoft template for the runsettings file. Note however that on my machine, the path mentioned in the comment in Line 5 is relative to the .runsettings location, not the .sln.
If you use /logger:trx option of vstest.console.exe, it will generate output in the same format as MSTest (good for result visualization).

TFS Build Machine Test method errors out on build machine

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.

Categories