Skip to content

philippleidig/TcUnit-VsTestAdapter

Repository files navigation

TcUnit logo GitHub license Open Source? Yes!

TcUnit-VsTestAdapter

TcUnit-VsTestAdapter makes it possible to execute TcUnit (TwinCAT unit tests) with the Microsoft Visual Studio Test Plattform

For more details see vstest test adapter extensibility

Table of Contents

Usage

This section describes how to execute TcUnit tests in Azure DevOps pipelines or locally with Microsoft.TestPlatform.

vstest.console.exe

It is also possible to run TcUnit tests on a VM that has vstest.console.exe installed.

Setup Microsoft.TestPlatform on a VM

Let's assume that the working folder is C:\Tools.

  1. Download and install NuGet.

  2. Install NuGet package Microsoft.TestPlatform

    nuget install Microsoft.TestPlatform
    

Find vstest.console.exe in C:\Tools\Microsoft.TestPlatform.16.7.1\tools\net451\Common7\IDE\Extensions\TestPlatform.

  1. Install TcUnit.TestAdapter

    nuget install TcUnit.TestAdapter
    
  2. List all available tests (optional)

    vstest.console.exe /ListTests /TestAdapterPath:C:\Tools\TcUnit.TestAdapter.1.0.0\lib\netstandard2.0 /Settings:path\to\TcUnit.runsettings *\*.tsproj
    
  3. Start test run and execute available tests

    vstest.console.exe /TestAdapterPath:C:\Tools\TcUnit.TestAdapter.1.0.0\lib\netstandard2.0 /Settings:path\to\TcUnit.runsettings *\*.tsproj
    
  4. You may now create a *.cmd / *.ps1 file, put it inside your TcUnit tests project and run it after compile

    twincat-plc-deployment.png

    See TwinCAT PLC Deployment for more information

Azure Pipelines

  1. Add NuGet tool installer task to install NuGet.exe.

    Example:

    steps:
    - task: NuGetToolInstaller@0
      displayName: 'Use NuGet'
      inputs:
        versionSpec: 4.4.1
  2. Add NuGet task. Set command to custom and specify the command line:

    install TcUnit.TestAdapter -Version $(TcUnitTestAdapterVersion)
    

    Example

    steps:
    - task: NuGetCommand@2
      displayName: 'NuGet Install TcUnit.TestAdapter'
      inputs:
        command: custom
        arguments: 'install TcUnit.TestAdapter -Version $(TcUnitTestAdapterVersion)'    

    In the pipeline settings set TcUnitTestAdapterVersion variable to the TcUnit.TestAdapter version you want to install (e.g. 1.0.0). The task will install TcUnit.TestAdapter into

    $(Build.Repository.LocalPath)\TcUnit.TestAdapter.$(TcUnitTestAdapterVersion)\lib\netstandard2.0
    
  3. If you plan to run tests on a self-hosted Windows Agent that does not have Visual Studio installed you need to add Visual Studio Test Platform Installer task.

    Example:

    steps:
    - task: VisualStudioTestPlatformInstaller@1
      displayName: 'Visual Studio Test Platform Installer'
      inputs:
        versionSelector: latestStable
  4. To run tests you need Visual Studio Test task.

    Example:

    steps:
    - task: VSTest@2
      displayName: 'VsTest - Run TcUnit Tests'
      inputs:
        testAssemblyVer2: |
         $(Build.Repository.LocalPath)\SolutionFolder\ProjectFolder\*.tsproj
        runSettingsFile: '$(System.DefaultWorkingDirectory)\.ci\TcUnit.runsettings'
        pathtoCustomTestAdapters: '$(Build.Repository.LocalPath)\TcUnit.TestAdapter.$(TcUnitTestAdapterVersion)\lib\netstandard2.0'
        platform: '$(BuildPlatform)'
        configuration: '$(BuildConfiguration)'

    Make sure that path to custom test set adapters is set to

    $(Build.Repository.LocalPath)\TcUnit.TestAdapter.$(TcUnitTestAdapterVersion)\lib\netstandard2.0
    
  5. To publish test results (for later review and downloading) use Publish Build Artifacts task. Execution results are copied to $(Agent.TempDirectory)\TestResults.

    Example:

    steps:
    - task: PublishBuildArtifacts@1
      displayName: 'Publish Artifact: TestResults'
      inputs:
        PathtoPublish: '$(Agent.TempDirectory)\TestResults'
        ArtifactName: TestResults

Best Practice

Filter

TcUnit.TestAdapter supports filter

e.g. only executes the specific test

vstest.console.exe /TestAdapterPath:C:\Tools\TcUnit.TestAdapter.1.0.0\lib\netstandard2.0 /Settings:path\to\TcUnit.runsettings *\*.tsproj /TestCaseFilter:"FullyQualifiedName=UnitTests.AxisGeneric_Tests.Should_Fail_When_AxisIdIsZero"

RunSettings

Use custom run settings

vstest.console.exe /Settings:path\to\TcUnit.runsettings

.runsettings file is used to pass parameters to the test run.

see docs for detailed information

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <RunConfiguration>
        <!-- Specify timeout in milliseconds. A valid value should be greater than 0 -->
        <TestSessionTimeout>20000</TestSessionTimeout>
    </RunConfiguration>
    <TcUnit>
        <Target>127.0.0.1.1.1</Target> 
        <CleanUpAfterTestRun>true</CleanUpAfterTestRun>
    </TcUnit>
</RunSettings>

About

Enables the execution of TcUnit via Visual Studio Test Plattform (https://github.com/microsoft/vstest)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published