Executing MSTests using Mono

Hello!
I’m trying to start utilizing GitLabs CI for my .NET project.
I’ve managed to successfully create a build job which passes without failure.
But I also want to run my tests.
The tests are written in MSTest.

How do I run a test?
The solutions I came across did not work - failed with the ‘command not found’ error message.

My .gitlab-ci.yaml:

image: mono:latest

stages:
  - build
  - test

build_job:
  stage: build
  script:
  - 'echo BUILDING'
  - 'echo restoring nuget...'
  - 'nuget restore Common/Common.sln'
  - 'echo building...'
  - 'MONO_IOMAP=case msbuild /t:Build /p:Configuration=Debug /p:Platform="Any CPU" Common/Common.sln'
  except:
  - tags

test_job:
  stage: test
  script:
  - 'echo TESTING'
  - 'echo restoring nuget...'
  - 'nuget restore Common/Common.sln'
  - 'echo building...'
  - 'MONO_IOMAP=case msbuild /t:Build /p:Configuration=Debug /p:Platform="Any CPU" Common/Common.sln'
  - 'echo Executing tests for:'
  - 'ls bin/Tests/Debug/*.dll | egrep ^*/UnitTestCommon*'
  - 'for i in $(ls bin/Tests/Debug/*.dll | egrep ^*/UnitTestCommon*); do mono nunit-console $i; done'
  except:
  - tags

Please note that I’m using a mono image.
The script attempts to run a test using the ‘mono nunit-console’ command, but as you may have already guessed it fails.

I’m very new to this and I’m probably missing something obvious here…

Any help/advice will be very much appreciated!
Thank you in advance.

P.S. Sorry if I’m missing any details which you may need to help me out. Please let me know if that’s the case and I’ll provide them asap.

I was fortunate enough to have found a solution on my own. Cost a lot of time but it was worth it.

The solution required me to migrate from MSTest to NUnit. So if you already have NUnit, you’re in luck!

Here is my full configuration, hope it helps someone out!

image: mono:latest

variables:
  PROJ_NAME: 'YourProjectName'
  BUILD_CONF: 'Debug'
  RELEASE_CONF: 'Release'

  UT_PREFIX: 'UnitTest'
  TEST_REPORT: 'TestResults.txt'

  TEST_RUNNER: 'NUnit.ConsoleRunner'
  TEST_RUNNER_W_VER: '${TEST_RUNNER}.3.9.0'
  PATH_TO_SOLUTION: '${PROJ_NAME}/${PROJ_NAME}.sln'

  # TODO Your paths will vary. I build all of my tests into one directory
  PATH_TO_RELEASE: 'bin/Libraries/${RELEASE_CONF}/'
  PATH_TO_TESTS: 'bin/Tests/${BUILD_CONF}/'

stages:
  - build
  - test
  - release

build_job:
  stage: build
  script:
  - 'nuget restore ${PATH_TO_SOLUTION}'
  - 'MONO_IOMAP=case msbuild /t:Build /p:Configuration=${BUILD_CONF} /p:Platform="Any CPU" ${PATH_TO_SOLUTION}'
  cache:
    key: binaries
    paths:
    - '${PATH_TO_TESTS}'
    policy: push
  except:
  - tags

test_job:
  stage: test
  cache:
    key: binaries
    paths:
    - '${PATH_TO_TESTS}'
    policy: pull
  script:
  # Update is required to install additional packages, qq for silent
  - 'apt-get -qq update -y'
  # ZIP is required for nupkg extraction
  - 'apt-get -qq install unzip'
  - 'nuget install ${TEST_RUNNER}'
  - 'unzip ${TEST_RUNNER_W_VER}/${TEST_RUNNER_W_VER}.nupkg'
  # You get rights and you get rights, you all get rights!
  - 'chmod -R 777 tools'

  # Black magic.png
  # 1. Runs testing for each .dll file
  # 2. Removes output header information
  # 3. Removes output run settings information
  # 4. Removes footer
  # 5. Writes to file
  - 'touch ${TEST_REPORT}'
  - for i in $(ls ${PATH_TO_TESTS}*.dll | egrep ^*/${UT_PREFIX}*); do mono tools/nunit3-console.exe $i | awk '/Test Files/'{f=1}f{print} | awk -v regex="Run Settings" -v count="8"  '$0 ~ regex { skip=count; next } --skip >= 0 { next } 1' | head -n -1 >> ${TEST_REPORT}; done
  - cat ${TEST_REPORT}
  artifacts:
    paths:
    - '${TEST_REPORT}'
    expire_in: 1 day
    when: always
  except:
  - tags

release_job:
 stage: release
 script:
  - 'nuget restore ${PATH_TO_SOLUTION}'
  - 'MONO_IOMAP=case msbuild /t:Build /p:Configuration=${RELEASE_CONF} /p:Platform="Any CPU" ${PATH_TO_SOLUTION}'
  - 'rm ${PATH_TO_RELEASE}*.pdb'
 artifacts:
   paths:
   - '${PATH_TO_RELEASE}'
   expire_in: 1 week
 when: manual

@hailstorm75 Hey , did you use Gitlab shared runners? I’m really struggling just to get anything to build

@jackso12 Please check out my yaml in this project

1 Like