> For the complete documentation index, see [llms.txt](https://jadelab.gitbook.io/jadegit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jadelab.gitbook.io/jadegit/0.18.0/ci-cd/pipeline/testing.md).

# Testing

The testing stage of the pipeline serves two purposes. First, it validates that a deployment can be built and successfully loaded, which provides early confidence that a release could be cut from the current branch. Second, it runs a suite of tests to validate application behaviour after the changes have been applied.

## Database

Testing is performed against an empty database image that reflects the last release, assuming they are updated following each release. The image includes a registry that records the current commit, which JadeGit uses as the prior version when building a deployment. The build then compares that commit against the latest commit on the branch being tested, producing a deployment representative of the next uncut release.

This means that if the deployment builds and loads successfully, it proves the changes can be applied to at least an empty database.

## Tests

Once the deployment has been loaded, the test suite is run against the database. How tests are executed goes beyond the scope of this documentation and is left to the implementation.

The use of an empty, ephemeral database may limit the scope of what can be tested at this stage. Some tests may depend on data or external services. Data-dependent tests can be supported through the preparation and use of a database image containing test data, while tests that depend on external services are better suited to running after changes have been deployed to an environment where those dependencies are available.

## Example

The `test.yml` template handles the full testing stage, downloading the database image and JadeGit, building and loading the deployment, running tests, and publishing results:

```yaml
parameters:
- name: database
  type: string
- name: platform
  type: object

steps:
- checkout: self
  persistCredentials: true
  
- template: ..\images\download.yml
  parameters:
    path: $(Agent.TempDirectory)\database
    config: empty
    database: ${{ parameters.database }}
    version: ${{ parameters.platform.version }}
    charset: ${{ parameters.platform.charset }}

- task: UniversalPackages@0
  displayName: "Download JadeGit"
  inputs:
    command: download
    vstsFeed: '$(jade.feed)'
    vstsFeedPackage: 'jadegit-dev-${{ parameters.platform.version }}-${{ parameters.platform.charset }}'
    vstsPackageVersion: '$(jadegit.version)'
    downloadDirectory: '$(Agent.TempDirectory)\database\bin'

- powershell: |
    @"
    # TODO: Add any config required for running tests
    "@ | Set-Content -path .\jade.ini
    & bin\jadegit -p "$pwd\system" -i $pwd\jade.ini registry extract "$(Agent.TempDirectory)\current.jgr"
  displayName: "Extract Registry"
  workingDirectory: $(Agent.TempDirectory)\database

- task: UniversalPackages@0
  displayName: "Download JadeGit Console"
  inputs:
    command: download
    vstsFeed: '$(jade.feed)'
    vstsFeedPackage: 'jadegit-console'
    vstsPackageVersion: '$(jadegit.version)'
    downloadDirectory: '$(Pipeline.Workspace)'

- powershell: |
    & $(Pipeline.Workspace)\jadegit registry show "$(Agent.TempDirectory)\current.jgr"
    & $(Pipeline.Workspace)\jadegit --log-progress-format "##vso[task.setprogress value={}]" build --registry "$(Agent.TempDirectory)\current.jgr" pwsh "$(Agent.TempDirectory)\build"
  displayName: "Build"

- task: PublishPipelineArtifact@1
  displayName: "Publish Build"
  inputs:
    targetPath: $(Agent.TempDirectory)\build
    artifact: test-${{ parameters.platform.version }}-${{ parameters.platform.charset }}-build
    publishLocation: 'pipeline'

- powershell: |
    & $(Agent.TempDirectory)\build\deploy -bin "$(Agent.TempDirectory)\database\bin" -path "$(Agent.TempDirectory)\database\system" -ini "$(Agent.TempDirectory)\database\jade.ini"
  displayName: "Load"

- powershell: |
    Write-Host "TODO: Add commands to run tests, next step assumes use of JUnit files to publish results"
  displayName: "Run Tests"
  workingDirectory: $(Agent.TempDirectory)\database
  
- task: PublishTestResults@2
  condition: succeeded()
  displayName: "Publish Test Results"
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '*-junit.xml'
    searchFolder: $(Agent.TempDirectory)\database\logs
    failTaskOnFailedTests: true

- task: PublishPipelineArtifact@1
  condition: succeededOrFailed()
  continueOnError: true
  displayName: "Publish Logs"
  inputs:
    targetPath: $(Agent.TempDirectory)\database\logs
    artifact: test-${{ parameters.platform.version }}-${{ parameters.platform.charset }}-logs
    publishLocation: 'pipeline'
```

{% hint style="info" %}
The first JadeGit download provides the binaries used to extract the registry from the database. This step can be omitted if up-to-date JadeGit binaries are already included in the database image.
{% endhint %}
