You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sdk/core/Azure.Core.TestFramework/README.md
+57-39Lines changed: 57 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
# Using the TestFramework
2
+
2
3
To start using the Test Framework, add a project reference using the alias `AzureCoreTestFramework` into your test `.csproj`:
3
4
4
5
```xml
@@ -9,8 +10,8 @@ To start using the Test Framework, add a project reference using the alias `Azur
9
10
...
10
11
11
12
</Project>
12
-
13
13
```
14
+
14
15
As an example, see the [Template](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/template/Azure.Template/tests/Azure.Template.Tests.csproj#L15) project.
15
16
16
17
## Sync-async tests
@@ -30,7 +31,6 @@ public class ConfigurationLiveTests: ClientTestBase
30
31
...,
31
32
InstrumentClientOptions(
32
33
newConfigurationClientClientOptions())));
33
-
}
34
34
35
35
publicasyncTaskDeleteSettingNotFound()
36
36
{
@@ -50,7 +50,6 @@ When using sync-async tests with recorded tests two sessions files will be gener
50
50
51
51
You can disable the sync-forwarding for an individual test by applying the `[AsyncOnly]` attribute to the test method.
52
52
53
-
54
53
__Limitation__: all method calls/properties that are being used have to be `virtual`.
55
54
56
55
## Test environment and live test resources
@@ -70,7 +69,7 @@ public class AppConfigurationTestEnvironment : TestEnvironment
70
69
}
71
70
```
72
71
73
-
**NOTE:** Make sure that variables containing secret values are not recorded or are sanitized.
72
+
__NOTE:__ Make sure that variables containing secret values are not recorded or are sanitized.
74
73
75
74
To sanitize variables use the `options` parameter of `GetRecordedVariable`:
76
75
@@ -147,31 +146,53 @@ public class AppConfigurationTestEnvironment : TestEnvironment
147
146
}
148
147
```
149
148
149
+
### Running live tests serially
150
+
151
+
By default, NUnit does not run tests within each assembly in parallel, but this be [configured](https://docs.nunit.org/articles/nunit/technical-notes/usage/Framework-Parallel-Test-Execution.html).
152
+
Especially for unit tests, this is often desirable; however, live and [recorded tests](#recorded-tests) may run into some issues. Thus, by default, the `RecordedTestBase` described below is attributed
153
+
as `[NonParallelizable]`.
154
+
155
+
However, when projects are built and tested in CIs, all projects are testing in parallel. This means, for example, you can have two or more assemblies running tests such as one backing up or restoring
156
+
a resource while another assembly's tests are trying to use that resource. The service may return an error like HTTP 409.
157
+
158
+
To isolate one or more projects so that they are tested serially, add a _service.projects_ file to your service directory e.g., _sdk/keyvault/service.projects_ with content like the following to set the
Test settings can be configured via `.runsettings` files. See [nunit.runsettings](https://github.com/Azure/azure-sdk-for-net/blob/main/eng/nunit.runsettings) for available knobs.
153
174
154
175
There are two ways to work with `.runsettings`. Both are picked up by Visual Studio without restart.
176
+
155
177
- You can edit [nunit.runsettings](https://github.com/Azure/azure-sdk-for-net/blob/main/eng/nunit.runsettings) locally to achieve desired configuration.
156
178
- You can prepare few copies of `.runsettings` by cloning [nunit.runsettings](https://github.com/Azure/azure-sdk-for-net/blob/main/eng/nunit.runsettings).
179
+
157
180
Load them in Visual Studio (`Test>Configure Run Settings` menu) and switch between them. This option requires setting an environment variable `AZURE_SKIP_DEFAULT_RUN_SETTINGS=true`.
158
181
159
182
## TokenCredential
160
183
161
184
If a test or sample uses `TokenCredential` to construct the client use `TestEnvironment.Credential` to retrieve it.
@@ -180,7 +201,6 @@ The test framework provides an ability to record HTTP requests and responses and
180
201
181
202
To use recorded test functionality inherit from `RecordedTestBase<T>` class and use `InstrumentClientOptions` method when creating the client instance. Pass the test environment class as a generic argument to `RecordedTestBase`.
@@ -215,7 +235,7 @@ In development scenarios where it's required to change mode quickly without rest
215
235
Recorded tests can be attributed with the `RecordedTestAttribute` in lieu of the standard `TestAttribute` to enable functionality to automatically re-record tests that fail due to recording session file mismatches.
216
236
Tests that are auto-rerecorded will fail with the following error and succeed if re-run.
217
237
218
-
```
238
+
```text
219
239
Error Message:
220
240
Test failed playback, but was successfully re-recorded (it should pass if re-run). Please copy updated recording to SessionFiles.
221
241
```
@@ -233,6 +253,7 @@ public class ConfigurationLiveTests: RecordedTestBase<AppConfigurationTestEnviro
233
253
}
234
254
}
235
255
```
256
+
236
257
In addition to the auto-rerecording functionality, using the RecordedTestAttribute also will automatically retry tests that fail due due to exceeding the global test time limit.
237
258
238
259
When running tests in `Live` or `Record` mode, the Test Framework will prompt you to create the live test resources required for the tests if you don't have environment variables or an env file containing the required variables needed for the tests. This means that you do not have to manually run the New-TestResources script when attempting to run live tests! The Test Framework will also attempt to automatically extend the expiration of the test resource resource group whenever live tests are run. If the resource group specified in your .env file or environment variable has already expired and thus been deleted, the framework will prompt you to create a new resource group just like it would if an env variable required by the test was missing.
@@ -244,19 +265,19 @@ When tests are run in recording mode, session records are saved to the project d
244
265
### Sanitizing
245
266
246
267
Secrets that are part of requests, responses, headers, or connections strings should be sanitized before saving the record.
247
-
**Do not check in session records containing secrets.** Common headers like `Authentication` are sanitized automatically, but if custom logic is required and/or if request or response body need to be sanitized, the `Sanitizer` property should be used as an extension point.
268
+
__Do not check in session records containing secrets.__ Common headers like `Authentication` are sanitized automatically, but if custom logic is required and/or if request or response body need to be sanitized, the `Sanitizer` property should be used as an extension point.
Another sanitization feature that is available involves sanitizing Json payloads.
@@ -265,21 +286,20 @@ By adding a [Json Path](https://www.newtonsoft.com/json/help/html/QueryJsonSelec
265
286
By default, the following values are added to the `JsonPathSanitizers` to be sanitized: `primaryKey`, `secondaryKey`, `primaryConnectionString`, `secondaryConnectionString`, and `connectionString`.
When tests are run in `Playback` mode, the HTTP method, Uri, and headers are used to match the request to the recordings. Some headers change on every request and are not controlled by the client code and should be ignored during matching. Common headers like `Date`, `x-ms-date`, `x-ms-client-request-id`, `User-Agent`, `Request-Id` are ignored by default but if more headers need to be ignored, use the various matching properties to customize as needed.
**Note:** If test recordings are enabled, the recordings will be generated against the latests version of the service.
396
+
__Note:__ If test recordings are enabled, the recordings will be generated against the latests version of the service.
377
397
378
398
## Support for an additional test parameter
379
399
@@ -395,6 +415,7 @@ public class TableServiceLiveTestsBase : RecordedTestBase<TablesTestEnvironment>
395
415
{
396
416
_endpointType=endpointType;
397
417
}
418
+
}
398
419
```
399
420
400
421
```c#
@@ -413,9 +434,10 @@ public class TableServiceLiveTestsBase : RecordedTestBase<TablesTestEnvironment>
413
434
_serviceVersion=serviceVersion;
414
435
_endpointType=endpointType;
415
436
}
437
+
}
416
438
```
417
439
418
-
**Note:** Additional parameter options work with test recordings and will create differentiated SessionRecords test classdirectory names for each additional parameter option.
440
+
__Note:__ Additional parameter options work with test recordings and will create differentiated SessionRecords test class directory names for each additional parameter option.
__NOTE:__ these scripts require being [signed in with Azure CLI](https://docs.microsoft.com/cli/azure/authenticate-azure-cli?view=azure-cli-latest) and access to the [internal DevOps project](https://dev.azure.com/azure-sdk/internal/).
453
475
454
476
### Note on private/non-virtual fields in your clients (such as _clientDiagnostics) and InternalsVisibleTo
455
477
@@ -490,8 +512,6 @@ There are various helpful classes that assist in writing tests for the Azure SDK
490
512
[TestEnvVar](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core.TestFramework/src/TestEnvVar.cs) allows you to wrap a block of code with a using statement inside which the configured Environment variables will be set to your supplied values.
491
513
It ensures that the existing value of any configured environment variables are preserved before they are set them and restores them outside the scope of the using block.
492
514
493
-
#### Example usage
494
-
495
515
```c#
496
516
using (var_=newTestEnvVar("AZURE_TENANT_ID", "foo"))
497
517
{
@@ -507,8 +527,6 @@ using (var _ = new TestEnvVar("AZURE_TENANT_ID", "foo"))
507
527
It ensures that the existing value of any configured switches are preserved before they are set them and restores them outside the scope of the using block.
508
528
Note: Even if an `AppContext` switch was un-set prior to setting it via `TestAppContextSwitch`, it will be unset after leaving the scope of the using block.
@@ -526,9 +544,9 @@ var isSet = AppContext.TryGetSwitch("Azure.Core.Pipeline.DisableHttpWebRequestTr
526
544
```
527
545
528
546
### AsyncAssert
547
+
529
548
This type contains static helper methods that cover some of the gaps in NUnit when it comes to async assertions. For instance, attempting to assert that a specific exception is thrown using Assert.That, Assert.Throws, or Assert.ThrowsAsync all result in sync over async code, which can lead to test flakiness.
0 commit comments