doc/dev/Using-Mock-Test-Generation.md
This article demonstrate how to generate and execute mock test for .Net SDK.
Writing and executing live tests for SDKs are difficult for some RPs:
In case we focused only on SDK behavior instead of service behavior, the mock test can be used to validate the SDK quality efficiently. The mock test can be generated and executed under the mock-service-host to save developer's efforts.
Below is a sample for mock testcase:
<div id="generate-mock-test"/>The Mgmt mock tests can be generated by below steps:
Go to folder Azure.ResourceManager.xxx\tests
> cd Azure.ResourceManager.xxx\tests
Create Azure.ResourceManager.xxx.Tests.csproj file. Below is a test project template file:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\src\Azure.ResourceManager.xxx.csproj" />
<PackageReference Include="Azure.Identity" />
</ItemGroup>
</Project>
Create file autorest.tests.md, the content are:
# Generated test code configuration
Run `dotnet build /t:GenerateTests` to generate test code.
``` yaml
require:
- ../src/autorest.md
```
Generate
[USE autorest]
// clone autorest.csharp and build
> autorest --use=PATH_TO_AUTOREST.CSHARP\artifacts\bin\AutoRest.CSharp\Debug\net6.0 autorest.tests.md --testmodeler={} --debug
[or USE dotnet target]
> dotnet build /t:GenerateTests
The generated results looks like below:
D:.
│ autorest.tests.md
│ Azure.ResourceManager.KeyVault.Tests.csproj
│
├───generated
│ │
│ └───Mock
│ DeletedManagedHsmCollectionTest.cs
│ DeletedManagedHsmTest.cs
│ DeletedVaultCollectionTest.cs
│ DeletedVaultTest.cs
│ ManagedHsmCollectionTest.cs
│ ManagedHsmTest.cs
│ MhsmPrivateEndpointConnectionCollectionTest.cs
│ MhsmPrivateEndpointConnectionTest.cs
│ PrivateEndpointConnectionCollectionTest.cs
│ PrivateEndpointConnectionTest.cs
│ TestHelper.cs
│ VaultCollectionTest.cs
│ VaultTest.cs
NOTE: The mock-service-host use OpenSSL Toolkit to create certificates for HTTPS channel. So make sure it's available in your computer.
The script Launch-MockServiceHost.ps1 can be used to launch the mock-service-host in one step. For example:
// Powershell Administrator Mode:
// launch mock-service-host for all RPs (take about 3 minutes):
> pwsh eng\scripts\Launch-MockServiceHost.ps1
// launch mock-service-host for single RP (take several seconds):
> pwsh eng\scripts\Launch-MockServiceHost.ps1 -rpName keyvault
The mock-service-host is ready after 'validator initialized' is shown.
> @azure-tools/[email protected] start D:\projects\codegen\azure-sdk-tools\tools\mock-service-host
> tsc && node dist/src/main.js
Listening https on port: 8441
Listening http on port: 8442
Listening https on port: 8443
Listening https on port: 8445
2021-12-21T07:54:10.265Z [info]: SpecRetrieverFilesystem: Set spec cache path as ../../../azure-rest-api-specs
2021-12-21T07:54:10.267Z [info]: validator is initializing with options {
"git": {
"url": "https://github.com/Azure/azure-rest-api-specs",
"shouldClone": false
},
"swaggerPathsPattern": [
"specification/keyvault/resource-manager/**/*.json"
],
"directory": "D:\\projects\\codegen\\azure-rest-api-specs",
"isPathCaseSensitive": false
}
2021-12-21T07:54:10.809Z [info]: validator initialized
<div id="execute-mock-test"/>NOTE: Refer to README.md for more information about mock-service-host.
NOTE: Don't need this step if once launched mock-service-host with Launch-MockServiceHost.ps1 in Administrator Mode.
The mock-service-host use a self-signed certificate to enable HTTPs channel, so need to trust the certificate before execute any mock test. Once the mock-service-host is launched, the certificate will be created as 'mock-service-host/.ssh/localhost-ca.crt'. Double click the crt file and follow below to install it.
Now you can execute the generated mock test like any others. All mock tests are generated in namespace 'Azure.ResourceManager.xxx.Tests.Mock', below is an picture for executing mock test in visual studio test explorer.
<div id="skip-swagger-example"/>NOTE: Recording files will be generated after the executing, need to include the recording files if want to commit the generated test into the sdk repo.
The test may be failed for tooling or example issues. You can skip the test generation for specific examples with configuration 'testmodeler.mock.disabled-examples' in autorest.tests.md
testmodeler:
mock:
disabled-examples:
- Create a virtual machine image from a managed disk with DiskEncryptionSet resource.
- Create a virtual machine image from a managed disk.
The example names can be copied from the generated test function:
<div id="FAQ"/>Q1: Does mock-test require service API readiness?
A: No. The mock test send request to local mock-service-host.
Q2: Are swagger example files required to generate the mock test?
A: Yes.
Q3: In the mock testcase, do I need to create a resource before I test the delete API?
A: No. The mock-service-host are stateless by default. Refer to mock-service-host for more information.