-
Notifications
You must be signed in to change notification settings - Fork 90
Writing End to End Tests using AzureTest
Travis Prescott edited this page Feb 18, 2022
·
8 revisions
AzureTest
is a static library written to help Azure SDK developers write tests for Swift that can be recorded and played back without network connectivity. It leverages a customized fork of the Venmo/DVR
framework, which itself is based on the popular Ruby VCR
framework.
You will need the following things installed:
- Powershell
- Bicep CLI (if using bicep files instead of ARM templates)
- XCode
- Swift 5+
- Within your framework's test targets, add
libAzureTest.a
underBuild Phases > Link Binary With Libraries
. - In
Package.swift
, addAzureTest
to the dependencies for your test target. - Within the test scheme for your target, ensure you have created and set the following environment variables:
-
TEST_MODE
: Set to either "record" or "playback". -
SDK_REPO_ROOT
: The local path to your clone ofazure-sdk-for-ios
.
Most tests require some Azure resources to be set up prior to running, and these resources should be cleaned up after the test completes. Mechanisms exists to facilitate this automatically.
- Within the service folder for your framework (the folder directly beneath
sdk
), add resources to either thetest-resources.bicep
file ortest-resources.json
. This file will be used to deploy resources when your test is run live (such as while recording). - If you need to do further setup with the resources created in step 1, add or modify the
test-resources-pre.ps1
ortest-resources-post.ps1
scripts to perform any necessary setup. By default, you can only use Powershell cmdlets. The necessary outputs from these scripts should be dumped into a file calledtest-settings.plist
(see below). - Run the following common engineering script from the root of the SDK repo to deploy your test resources:
./eng/common/TestResources/New-TestResources.ps1 <SERVICE_DIR>
- Create a class which implements
AzureTest.TestSettingsProtocol
. This model should consist of string-based properties which back credentials you need to supply to run tests live. - Within your
Tests
folder, add the plist file created in step 3 above to the project. The file MUST MUST be namedtest-settings.plist
, as this filename is part of the.gitignore
and thus will not be committed as a change in source control. - Ensure that
test-settings.plist
has target membership in the relevant test target(s).
- Within the
Tests
folder, create a new "Unit Test Case Class" in Xcode and importAzureTest
along with any other necessary imports. - Change the test class to inherit from
RecordableXCTestCase
instead ofXCTestCase
and supply the name of your settings class as the generic argument (ex:class MyFirstTest: RecordableXCTestCase<MyTestSettings> { ... }
). - Implement the
setUpTest
and/orsetUpTestWithError
method to load any values from your settings and set up your client in order to make service calls. - Use your client to make service calls like you normally would. After you first run your tests in record mode, you must add the recording files to your project. Otherwise, the files will not be found when you attempt to play them back. Once the files have been added initially, re-recording tests will automatically update them.
- Change the
TEST_MODE
environment variable in your target's Test scheme to "playback". - Run the tests. They should complete much faster and without any network connectivity.
- Remove the
test-settings.plist
reference from your test folder before committing it or you will get a build error that the input file is not found.
- If playback mode fails, ensure you have added the recording files to the project.
- If you get a build error when creating a PR, ensure you have removed the reference to
test-setting.plist
from your project.