c# - 如何使用 C# 从 Visual Studio 传递管道变量

标签 c# powershell variables azure-devops azure-pipelines

我正在尝试在 Azure 管道中运行一些 nunit 测试。我使用 nunit 脚本中的测试运行设置传递了一些测试参数,并且也在 Pipeline 中定义了这些变量。测试后,我必须根据测试输出更改Azure管道中的变量,该变量可以在后续脚本中使用。我尝试了多种方法,但似乎没有任何结果。我确实尝试在 Pipeline 中使用 powershell 命令自行设置管道变量,但当我尝试从 TestAssemblies(C# 代码)中执行相同操作时,它不起作用。

YAML 代码

pool:
  name: New Agent Pool
  demands: vstest

variables:
  sauce: 'tomato'
  sauce1: 'something'

steps:
- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: '$(Parameters.solution)'
  enabled: false

- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    solution: '$(Parameters.solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'
  enabled: false

- powershell: |
   # Write your PowerShell commands here.

   Write-Output sauce = $(sauce)
   Write-Output sauce1 = $(sauce1)

  displayName: 'PowerShell Script'

- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*test*.dll
     !**\obj\**
    testFiltercriteria: 'Name=UnitTest1'
    runSettingsFile: SeleniumTest.ABC/Test.runsettings
    overrideTestrunParameters: '-sauce $(sauce)'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- powershell: |
   # Write your PowerShell commands here.

   Write-Output sauce = $(sauce)
   Write-Output sauce1 = $(sauce1)

  displayName: 'PowerShell Script'
    [Test]
    //[Category ("Google")]
    public void UnitTest1()
    {
        string sauce = TestContext.Parameters["sauce"];
        string sauce1 = TestContext.Parameters["sauce1"];
        TestContext.Progress.WriteLine(sauce);
        TestContext.Progress.WriteLine(sauce1);
        string text = "Write-Output '##vso[task.setvariable variable=sauce;isOutput=true]crushed tomatoes'";
        string op = RunScript(text);
        TestContext.WriteLine(op);
    }


    private string RunScript(string scriptText)
    {
        // create Powershell runspace
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        // create a pipeline and feed it the script text
        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(scriptText);

        Collection<PSObject> results = pipeline.Invoke();
        // close the runspace
        runspace.Close();

        // convert the script result into a single string
        StringBuilder stringBuilder = new StringBuilder();
        foreach (PSObject obj in results)
        {
            stringBuilder.AppendLine(obj.ToString());
        }
        return stringBuilder.ToString();
    }

最佳答案

您使用复杂的方式通过 powershell 执行来打印消息。您可以使用TestContext.Progress.WriteLine:

    [Test]
    public void Test1()
    {
        TestContext.Progress.WriteLine("##vso[task.setvariable variable=sauce]crushed tomatoes test project");
        Assert.Pass();
    }

这是我的结果:

enter image description here

另外检查测试的路径。如果您使用网络核心,则可以是这样的:

- task: VSTest@2

  displayName: 'VsTest - testAssemblies'

  inputs:

    testAssemblyVer2: |
     **\$(BuildConfiguration)\netcoreapp3.1\YourTestLib.dll
     !**\obj\**

    platform: '$(BuildPlatform)'

    configuration: '$(BuildConfiguration)'

关于c# - 如何使用 C# 从 Visual Studio 传递管道变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384966/

相关文章:

c# - 如何打开文件进行独立读/写?

regex - PowerShell regex -match 只匹配一次

c# - Powershell 自定义 cmdlet 在标准方法的实现之外调用 WriteVerbose

SQL语句中的Python变量?

python:导入的模块处于不同的上下文中

c# - 使用多个表中的数据创建下拉列表

c# - 从代码 C# 更改 Web.Config 中的 URL 重写规则

javascript - Fullcalendar:如何从两个变量获取事件并通过 'eventAfterRender' 传递?

c# - 多态性能受到影响

powershell - If/ElseIf block 不适用于 -or