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
Related
console app with a batch file which will hit my Automation testing application and runs selected test cases. I have test case with code coverage and it runs from my visual studio . and now on top of it I have to create a console app which will keep some time interval and hit my VS test case and execute it.Any links will be helpful.
I expect the test case pass and fails status
Here's the batch file I use to run my Selenium tests with multiple runsettings files.
#ECHO OFF
IF NOT EXIST Results MKDIR Results
SETLOCAL
SET PATH=%PATH%;"C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\TestPlatform"
SET VSTEST=vstest.console.exe
SET TESTS=.\bin\Debug\something.something.Tests.dll
IF "%~1"=="" (
FOR %%J IN (*.runsettings) DO CALL :func %%J
) ELSE (
CALL :func "%~1"
)
GOTO :EOF
:func
ECHO.
ECHO %~1 **********
ECHO.
"%VSTEST%" %TESTS% /Settings:"%~1" /Logger:trx /ResultsDirectory:Results
GOTO :EOF
You could use NUNit's console runner to run tests from the command line, or on a build server. I've had great success with running my tests this way.
First, you need to install the NUnit.ConsoleRunner Nuget package onto your project.
Then, navigate to the NUnit.ConsoleRunner directory under your packages folder that exists in the project directory.
Open NUnit.ConsoleRunner > tools folder to get into the same directory as the .exe itself.
Then, you can run:
nunit3-console {Path to your project's .dll} --testlist={Path to .txt testlist}
With valid parameters, it looks something like this:
nunit3-console C:\Users\christine.harbour\Repository\AutomationTestSuite\AutomationTestSuite.dll --testlist=C:\Users\christine.harbour\Repository\AutomationTestSuite\MyTestList.txt
Your testlist should contain namespaces of the test cases you wish to run, separated by a line break. For example:
AutomationTestSuite.Tests.MyTestClass_1.MyTest
AutomationTestSuite.Tests.MyTestClass_2.MyOtherTest
After you run the tests, the results will be saved in the NUnit.ConsoleRunner > tools directory. The results are in XML format and can be programatically parsed to push your test results to another tool.
There are plenty of arguments you can pass into the ConsoleRunner, including build configuration and framework version, all which are specified on NUnit's documentation.
NUnit console runner also integrates with Cake, which is a build scripting tool for C# projects. So, you could hypothetically clean / build your project, restore missing package references, and run your tests, all from the console.
More info on NUnit console runner can be found here: https://github.com/nunit/docs/wiki/Console-Command-Line
I use Jenkins, for building, unit testing, and publishing a windows application in c#.
While executing test cases using nunit-console.exe, some of them needs Application.Executing Path for completing the test case.
And it returns, nunit-console.exe path as result. Hence these test cases getting failed. ( What i need here is C# application executing path)
How can i solve this issue.
Environment.CurrentDirectory
returns the current working directory.
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 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).
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;