GivenDotnetTestBuildsAndRunsTestfromCsprojWithCorrectTestRunParameters.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Runtime.CompilerServices;
  4. using Microsoft.DotNet.Cli.Utils;
  5. using Microsoft.DotNet.Tools.Test.Utilities;
  6. namespace Microsoft.DotNet.Cli.Test.Tests
  7. {
  8. public class GivenDotnetTestBuildsAndRunsTestfromCsprojWithCorrectTestRunParameters : SdkTest
  9. {
  10. public GivenDotnetTestBuildsAndRunsTestfromCsprojWithCorrectTestRunParameters(ITestOutputHelper log) : base(log)
  11. {
  12. }
  13. private readonly string[] ConsoleLoggerOutputNormal = new[] { "--logger", "console;verbosity=normal" };
  14. [Fact]
  15. public void GivenAProjectAndMultipleTestRunParametersItPassesThemToVStestConsoleInTheCorrectFormat()
  16. {
  17. var testProjectDirectory = CopyAndRestoreVSTestDotNetCoreTestApp("2");
  18. // Call test
  19. CommandResult result = new DotnetTestCommand(Log, disableNewOutput: true)
  20. .WithWorkingDirectory(testProjectDirectory)
  21. .Execute(ConsoleLoggerOutputNormal.Concat(new[] {
  22. "--",
  23. "TestRunParameters.Parameter(name=\"myParam\",value=\"value\")",
  24. "TestRunParameters.Parameter(name=\"myParam2\",value=\"value with space\")"
  25. }));
  26. // Verify
  27. if (!TestContext.IsLocalized())
  28. {
  29. result.StdOut.Should().NotMatch("The test run parameter argument '*' is invalid.");
  30. result.StdOut.Should().Contain("Total tests: 1");
  31. result.StdOut.Should().Contain("Passed: 1");
  32. result.StdOut.Should().Contain("Passed VSTestTestRunParameters");
  33. }
  34. result.ExitCode.Should().Be(0);
  35. }
  36. [Fact]
  37. public void GivenADllAndMultipleTestRunParametersItPassesThemToVStestConsoleInTheCorrectFormat()
  38. {
  39. var testProjectDirectory = CopyAndRestoreVSTestDotNetCoreTestApp("3");
  40. var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
  41. new BuildCommand(Log, testProjectDirectory)
  42. .Execute()
  43. .Should().Pass();
  44. var outputDll = Path.Combine(OutputPathCalculator.FromProject(testProjectDirectory).GetOutputDirectory(configuration: configuration), "VSTestTestRunParameters.dll");
  45. // Call test
  46. CommandResult result = new DotnetTestCommand(Log, disableNewOutput: false)
  47. .Execute(ConsoleLoggerOutputNormal.Concat(new[] {
  48. outputDll,
  49. "--",
  50. "TestRunParameters.Parameter(name=\"myParam\",value=\"value\")",
  51. "TestRunParameters.Parameter(name=\"myParam2\",value=\"value with space\")"
  52. }));
  53. // Verify
  54. if (!TestContext.IsLocalized())
  55. {
  56. result.StdOut.Should().NotMatch("The test run parameter argument '*' is invalid.");
  57. result.StdOut.Should().Contain("Total tests: 1");
  58. result.StdOut.Should().Contain("Passed: 1");
  59. result.StdOut.Should().Contain("Passed VSTestTestRunParameters");
  60. }
  61. result.ExitCode.Should().Be(0);
  62. }
  63. private string CopyAndRestoreVSTestDotNetCoreTestApp([CallerMemberName] string callingMethod = "")
  64. {
  65. // Copy VSTestCore project in output directory of project dotnet-vstest.Tests
  66. string testAppName = "VSTestTestRunParameters";
  67. var testInstance = _testAssetsManager.CopyTestAsset(testAppName, callingMethod: callingMethod)
  68. .WithSource()
  69. .WithVersionVariables();
  70. var testProjectDirectory = testInstance.Path;
  71. // Restore project VSTestCore
  72. new RestoreCommand(testInstance)
  73. .Execute()
  74. .Should()
  75. .Pass();
  76. return testProjectDirectory;
  77. }
  78. }
  79. }