Skip to content

Commit d930806

Browse files
authored
Add readme docs for TestEnvVar and TestAppContextSwitch (Azure#20629)
1 parent 48c1b35 commit d930806

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

sdk/core/Azure.Core.TestFramework/README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,13 @@ public class ConfigurationLiveTests: RecordedTestBase<AppConfigurationTestEnviro
227227
}
228228
```
229229

230-
231-
## Recording
230+
### Recording
232231

233232
When tests are run in recording mode, session records are saved to the project directory automatically in a folder named 'SessionRecords'.
234233

235234
__NOTE:__ recordings are copied from `netcoreapp2.1` directory by default, make sure you are running the right target framework.
236235

237-
## Sanitizing
236+
### Sanitizing
238237

239238
Secrets that are part of requests, responses, headers, or connections strings should be sanitized before saving the record.
240239
**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 `RecordedTestSanitizer` should be used as an extension point.
@@ -271,7 +270,7 @@ Another sanitizer property that is available for sanitizing Json payloads is the
271270
}
272271
```
273272

274-
## Matching
273+
### Matching
275274

276275
When tests are run in replay mode, 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 the 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 `RecordMatcher` extensions point.
277276

@@ -295,7 +294,7 @@ When tests are run in replay mode, HTTP method, Uri and headers are used to matc
295294
}
296295
```
297296

298-
## Misc
297+
### Misc
299298

300299
You can use `Recording.GenerateId()` to generate repeatable random IDs.
301300

@@ -309,7 +308,7 @@ You can use `if (Mode == RecordingMode.Playback) { ... }` to change behavior for
309308

310309
You can use `using (Recording.DisableRecording()) { ... }` to disable recording in the code block (useful for polling methods)
311310

312-
# Support multi service version testing
311+
## Support multi service version testing
313312

314313
To enable multi-version testing, add the `ClientTestFixture` attribute listing to all the service versions to the test class itself or a base class:
315314

@@ -402,6 +401,7 @@ For example:
402401
`/SessionRecords/TableClientLiveTests(Storage)/CreatedCustomEntitiesCanBeQueriedWithFiltersAsync.json`
403402

404403
## Management libraries
404+
405405
Testing of management libraries uses the Test Framework and should generally be very similar to tests that you write for data plane libraries. There is an intermediate test class that you will likely want to derive from that lives within the management code base - [ManagementRecordedTestBase](https://github.com/Azure/azure-sdk-for-net/blob/babee31b3151e4512ac5a77a55c426c136335fbb/common/ManagementTestShared/ManagementRecordedTestBase.cs). To see examples of Track 2 Management tests using the Test Framework, take a look at the [Storage tests](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/storage/Azure.ResourceManager.Storage/tests/Tests).
406406

407407
## Recording tests on CI
@@ -428,7 +428,7 @@ The `Download-DevOpsRecordings.ps1` would wait for active runs to finish before
428428

429429
**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/)
430430

431-
## Note on private/non-virtual fields in your clients (such as _clientDiagnostics) and InternalsVisibleTo
431+
### Note on private/non-virtual fields in your clients (such as _clientDiagnostics) and InternalsVisibleTo
432432

433433
Some bindings require code on the customized side to access fields that are generated. For example:
434434

@@ -457,3 +457,47 @@ For this to work with tests, your test class must have an `InternalsVisisbleTo`
457457
```
458458

459459
If this is neglected, _clientDiagnostics will be null at test runtime.
460+
461+
## Miscellaneous Helpers
462+
463+
There are various helpful classes that assist in writing tests for the Azure SDK. Below are some of them.
464+
465+
### TestEnvVar
466+
467+
[TestEnvVar](https://github.com/Azure/azure-sdk-for-net/blob/master/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.
468+
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.
469+
470+
#### Example usage
471+
472+
```c#
473+
using (var _ = new TestEnvVar("AZURE_TENANT_ID", "foo"))
474+
{
475+
// Test code that relies on the value of AZURE_TENANT_ID
476+
}
477+
478+
// The previous value of AZURE_TENANT_ID is set again here.
479+
```
480+
481+
### TestAppContextSwitch
482+
483+
[TestAppContextSwitch](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/core/Azure.Core.TestFramework/src/TestAppContextSwitch.cs) allows you to wrap a block of code with a using statement inside which the configured [AppContext](https://docs.microsoft.com/dotnet/api/system.appcontext) switch will be set to your supplied values.
484+
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.
485+
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.
486+
487+
#### Example usage
488+
489+
```c#
490+
var isSet = AppContext.TryGetSwitch("Azure.Core.Pipeline.DisableHttpWebRequestTransport", out val))
491+
// isSet is false
492+
493+
using (var _ = new TestAppContextSwitch("Azure.Core.Pipeline.DisableHttpWebRequestTransport", "true"))
494+
{
495+
var isSet = AppContext.TryGetSwitch("Azure.Core.Pipeline.DisableHttpWebRequestTransport", out val))
496+
// isSet is true
497+
// val is true
498+
499+
}
500+
501+
var isSet = AppContext.TryGetSwitch("Azure.Core.Pipeline.DisableHttpWebRequestTransport", out val))
502+
// isSet is false
503+
```

0 commit comments

Comments
 (0)