繁体   English   中英

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

[英]How to Pass Pipeline Variables from Visual Studio using C#

我正在尝试在 Azure 管道中运行一些 nunit 测试。 我使用 nunit 脚本中的测试运行设置传递了一些测试参数,并且还在 Pipleline 中定义了这些变量。 经过测试,我必须根据测试输出更改Azure管道中的变量,以便在后续脚本中使用。 我尝试了多种方法,但似乎没有任何效果。 我确实尝试在 Piepline 中使用 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();
    }

这是我的结果:

在此处输入图片说明

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

- task: VSTest@2

  displayName: 'VsTest - testAssemblies'

  inputs:

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

    platform: '$(BuildPlatform)'

    configuration: '$(BuildConfiguration)'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM