GivenDotnetTestBuildsAndRunsTestFromDll.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Microsoft.DotNet.Tools.Test.Utilities;
  4. namespace Microsoft.DotNet.Cli.Test.Tests
  5. {
  6. public class GivenDotnettestBuildsAndRunsTestFromDll : SdkTest
  7. {
  8. public GivenDotnettestBuildsAndRunsTestFromDll(ITestOutputHelper log) : base(log)
  9. {
  10. }
  11. private readonly string[] ConsoleLoggerOutputNormal = new[] { "--logger", "console;verbosity=normal" };
  12. [Fact]
  13. public void TestsFromAGivenContainerShouldRunWithExpectedOutput()
  14. {
  15. var testAppName = "VSTestCore";
  16. var testAsset = _testAssetsManager.CopyTestAsset(testAppName)
  17. .WithSource()
  18. .WithVersionVariables();
  19. var testRoot = testAsset.Path;
  20. var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
  21. var buildCommand = new BuildCommand(testAsset);
  22. buildCommand
  23. .Execute()
  24. .Should().Pass();
  25. var outputDll = Path.Combine(buildCommand.GetOutputDirectory(configuration: configuration).FullName, $"{testAppName}.dll");
  26. // Call vstest
  27. var result = new DotnetTestCommand(Log, disableNewOutput: false)
  28. .Execute(outputDll, "--logger:console;verbosity=normal");
  29. if (!TestContext.IsLocalized())
  30. {
  31. result.StdOut
  32. .Should().Contain("Total tests: 2")
  33. .And.Contain("Passed: 1")
  34. .And.Contain("Failed: 1")
  35. .And.Contain("Passed VSTestPassTest")
  36. .And.Contain("Failed VSTestFailTest");
  37. }
  38. result.ExitCode.Should().Be(1);
  39. }
  40. [Fact]
  41. public void ItSetsDotnetRootToTheLocationOfDotnetExecutableWhenRunningDotnetTestWithDll()
  42. {
  43. var testAppName = "VSTestCore";
  44. var testAsset = _testAssetsManager.CopyTestAsset(testAppName)
  45. .WithSource()
  46. .WithVersionVariables();
  47. var testRoot = testAsset.Path;
  48. var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
  49. new BuildCommand(testAsset)
  50. .Execute()
  51. .Should().Pass();
  52. var outputDll = Path.Combine(testRoot, "bin", configuration, ToolsetInfo.CurrentTargetFramework, $"{testAppName}.dll");
  53. // Call dotnet test + dll
  54. var result = new DotnetTestCommand(Log, disableNewOutput: true)
  55. .Execute(outputDll, "--logger:console;verbosity=normal");
  56. result.ExitCode.Should().Be(1);
  57. var dotnet = result.StartInfo.FileName;
  58. Path.GetFileNameWithoutExtension(dotnet).Should().Be("dotnet");
  59. string dotnetRoot = Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)";
  60. result.StartInfo.EnvironmentVariables.ContainsKey(dotnetRoot).Should().BeTrue($"because {dotnetRoot} should be set");
  61. result.StartInfo.EnvironmentVariables[dotnetRoot].Should().Be(Path.GetDirectoryName(dotnet));
  62. }
  63. [Fact]
  64. public void TestsFromAGivenContainerAndArchSwitchShouldFlowToVsTestConsole()
  65. {
  66. var testAppName = "VSTestCore";
  67. var testAsset = _testAssetsManager.CopyTestAsset(testAppName)
  68. .WithSource()
  69. .WithVersionVariables();
  70. var testRoot = testAsset.Path;
  71. var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
  72. new BuildCommand(testAsset)
  73. .Execute()
  74. .Should().Pass();
  75. var outputDll = Path.Combine(testRoot, "bin", configuration, ToolsetInfo.CurrentTargetFramework, $"{testAppName}.dll");
  76. // Call vstest
  77. var result = new DotnetTestCommand(Log, disableNewOutput: true)
  78. .Execute(outputDll, "--arch", "wrongArchitecture");
  79. if (!TestContext.IsLocalized())
  80. {
  81. result.StdErr.Should().StartWith("Invalid platform type: wrongArchitecture. Valid platform types are ");
  82. }
  83. result.ExitCode.Should().Be(1);
  84. }
  85. [Theory]
  86. [InlineData("-e:foo=bardll")]
  87. [InlineData("-e:foo=barexe")]
  88. public void MissingOutputDllAndArgumentsEndWithDllOrExeShouldFailInMSBuild(string arg)
  89. {
  90. var testAppName = "VSTestCore";
  91. var testAsset = _testAssetsManager.CopyTestAsset(testAppName)
  92. .WithSource()
  93. .WithVersionVariables();
  94. new BuildCommand(testAsset)
  95. .Execute()
  96. .Should().Pass();
  97. var result = new DotnetTestCommand(Log, disableNewOutput: true)
  98. .Execute(arg);
  99. if (!TestContext.IsLocalized())
  100. {
  101. result.StdOut.Should().Contain("MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.");
  102. }
  103. result.ExitCode.Should().Be(1);
  104. }
  105. }
  106. }