Skip to content

Commit 90c1b81

Browse files
authored
Track1 testfx re-design and improvement (#18720)
* Track1 testfx re-design and improvement * Update azure-powershell-developer-guide.md * Update using-azure-test-framework.md
1 parent 00e1375 commit 90c1b81

File tree

188 files changed

+5705
-4323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+5705
-4323
lines changed

documentation/development-docs/azure-powershell-developer-guide.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,58 @@ _Note_: As mentioned in the prerequisites section, set the PowerShell [execution
248248

249249
## Using Azure TestFramework
250250

251-
Please see our guide on [Using Azure TestFramework](../testing-docs/using-azure-test-framework.md) for information on how to setup the appropriate connection string and record tests using the `Microsoft.Rest.ClientRuntime.Azure.TestFramework` package.
251+
Please see our guide on [Using Azure TestFramework](../testing-docs/using-azure-test-framework.md) for information on how to setup the appropriate connection string and record tests.
252252

253253
## Scenario Tests
254254

255+
### Adding Test Project
256+
257+
- Create a new folder called `ScenarioTests`
258+
- Create a new folder called `SessionRecords`
259+
- Inside the `ScenarioTests` folder, create a new class called `<SERVICE>TestRunner`
260+
- In the `<SERVICE>TestRunner` class, it should have the similar field and constructor like shown below. The parameter values passed in are based on the real situation.
261+
```csharp
262+
protected readonly ITestRunner TestRunner;
263+
264+
protected <SERVICE>TestRunner(ITestOutputHelper output)
265+
{
266+
TestRunner = TestManager.CreateInstance(output)
267+
.WithProjectSubfolderForTests("ScenarioTests")
268+
.WithNewPsScriptFilename($"{GetType().Name}.ps1")
269+
.WithCommonPsScripts(new[]
270+
{
271+
@"Common.ps1",
272+
@"../AzureRM.Resources.ps1",
273+
@"../AzureRM.Storage.ps1"
274+
})
275+
.WithNewRmModules(helper => new[]
276+
{
277+
helper.RMProfileModule,
278+
...
279+
})
280+
.WithNewRecordMatcherArguments(
281+
userAgentsToIgnore: new Dictionary<string, string>
282+
{
283+
...
284+
},
285+
resourceProviders: new Dictionary<string, string>
286+
{
287+
...
288+
}
289+
)
290+
.Build();
291+
}
292+
```
293+
255294
### Adding Scenario Tests
256295

257296
- Create a new class in `<SERVICE>.Test`
297+
- The new class must inherit from the `<SERVICE>TestRunner` class in this project.
258298
- Add `[Fact]` as an attribute to every test
259299
- Add `[Trait(Category.AcceptanceType, Category.CheckIn)]` as an attribute to any test that should be run during CI in Playback mode.
260300
- Add `[Trait(Category.AcceptanceType, Category.LiveOnly)]` as an attribute to any test that cannot be run in Playback mode (for example, if a test depends on a Dataplane SDK).
261-
- Create a ps1 file in the same folder that contains the actual tests ([see sample](../../src/Media/Media.Test/ScenarioTests))
301+
- Create a ps1 file in the same folder that contains the actual tests.
302+
- The name of the ps1 file should exactly match with name of the class. ([see sample](../../src/Media/Media.Test/ScenarioTests))
262303
- Use `Assert-AreEqual x y` to verify that values are the same
263304
- Use `Assert-AreNotEqual x y` to verify that values are not the same
264305
- Use `Assert-Throws scriptblock message` to verify an exception is being thrown
@@ -302,10 +343,7 @@ Create this environment variables for the AD scenario tests:
302343
### Recording/Running Tests
303344

304345
- Set up environment variables using New-TestCredential as described [here](../testing-docs/using-azure-test-framework.md#new-testcredential)
305-
- Run the test in Visual Studio in the Test Explorer window and make sure you got a generated JSON file that matches the test name in the bin folder under the `SessionRecords` folder
306-
- Copy this `SessionRecords` folder and place it inside the test project
307-
- Inside Visual Studio, add all of the generated JSON files, making sure to change the "Copy to Output Directory" property for each one to "Copy if newer"
308-
- Make sure that all of these JSON files appear in your `<SERVICE>.Test.csproj` file
346+
- Run the test in Visual Studio in the Test Explorer window and make sure you got a generated JSON file that matches the test name under the `SessionRecords` folder
309347

310348
# After Development
311349

documentation/testing-docs/using-azure-test-framework.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
- Import the `Repo-Tasks` module that helps to perform basic repository tasks
2929
- Run the command `Import-Module .\Repo-Tasks.psd1`
3030

31-
## Acquiring TestFramework
31+
## Azure PowerShell TestFramework
3232

33-
The `TestFramework` library is available on NuGet at https://www.nuget.org/packages/Microsoft.Rest.ClientRuntime.Azure.TestFramework/ .
34-
35-
Instructions on manually downloading the library are available on NuGet, however, `TestFramework` will automatically be downloaded as part of the build process, so the manual download is usually not necessary.
33+
Azure PowerShell repo now has its own test framework located under `tools\TestFx`, which supports recording all the HTTP requests from behind Azure PowerShell cmdlets and then playing them back.
3634

3735
The target framework of test is .Net Core 3.1, please ensure .Net runtime Microsoft.NETCore.App 3.1 is installed. You can list all installed version via `dotnet --info`.
3836

@@ -129,8 +127,7 @@ AZURE_TEST_MODE=Record
129127

130128
## Record or Playback Tests
131129

132-
- [Run the tests](https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/azure-powershell-developer-guide.md#recordingrunning-tests) and make sure that you got a generated `.json` file that matches the test name in the bin folder under the `SessionRecords` folder
133-
- Copy the `SessionRecords` folder inside the test project and add all `*.json` files in Visual Studio setting "Copy to Output Directory" property to "Copy if newer"
130+
- [Run the tests](https://github.com/Azure/azure-powershell/blob/main/documentation/development-docs/azure-powershell-developer-guide.md#recordingrunning-tests) and make sure that you got a generated `.json` file that matches the test name under the `SessionRecords` folder in the test project.
134131
- To assure that the records work fine, delete the connection string (default mode is Playback mode) OR change HttpRecorderMode within the connection string to "Playback" and run the tests
135132

136133
## Change Test Environment settings at run-time

src/Accounts/Accounts.Test/AutosaveTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using Microsoft.Azure.Commands.Profile.Context;
2626
using Microsoft.Azure.Commands.ScenarioTest;
2727
using Microsoft.Azure.Commands.ResourceManager.Common;
28+
using Microsoft.Azure.Commands.TestFx.Mocks;
2829

2930
namespace Microsoft.Azure.Commands.Profile.Test
3031
{

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Azure.Commands.Profile.Models;
2222
using Microsoft.Azure.Commands.Profile.Test;
2323
using Microsoft.Azure.Commands.ScenarioTest;
24+
using Microsoft.Azure.Commands.TestFx.Mocks;
2425
using Microsoft.Azure.Management.ResourceManager.Version2021_01_01.Models;
2526
using Microsoft.Azure.ServiceManagement.Common.Models;
2627
using Microsoft.Rest;
@@ -61,11 +62,11 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
6162
Guid.NewGuid().ToString(), DefaultTenant.ToString());
6263
var subscriptionList = new Queue<List<string>>(subscriptionLists);
6364
var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);
64-
var mock = new MockClientFactory(new List<object>
65+
var mock = new MockClientFactory(null, new List<object>
6566
{
6667
clientFactory.GetSubscriptionClientVerLatest(),
6768
clientFactory.GetSubscriptionClientVer2016()
68-
}, true);
69+
});
6970
mock.MoqClients = true;
7071
AzureSession.Instance.ClientFactory = mock;
7172
var sub = new AzureSubscription()
@@ -89,11 +90,11 @@ private static AzureRmProfile SetupLogin(List<string> tenants, params List<strin
8990
AzureSession.Instance.AuthenticationFactory = new AuthenticationFactory();
9091
var subscriptionList = new Queue<List<string>>(subscriptionLists);
9192
var clientFactory = new MockSubscriptionClientFactory(tenants, subscriptionList);
92-
var mock = new MockClientFactory(new List<object>
93+
var mock = new MockClientFactory(null, new List<object>
9394
{
9495
clientFactory.GetSubscriptionClientVerLatest(),
9596
clientFactory.GetSubscriptionClientVer2016()
96-
}, true);
97+
});
9798
mock.MoqClients = true;
9899
AzureSession.Instance.ClientFactory = mock;
99100
var sub = new AzureSubscription()

src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Azure.Commands.Profile.Models;
1919
using Microsoft.Azure.Commands.Profile.Test.Mocks;
2020
using Microsoft.Azure.Commands.ScenarioTest;
21+
using Microsoft.Azure.Commands.TestFx.Mocks;
2122
using Microsoft.Azure.ServiceManagement.Common.Models;
2223
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2324
using Microsoft.WindowsAzure.Commands.ScenarioTest;

src/Accounts/Accounts.Test/ContextCmdletTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Models;
17-
using Microsoft.Azure.Commands.Profile;
18-
using Microsoft.Azure.Commands.Profile.Models;
1917
// TODO: Remove IfDef
2018
#if NETSTANDARD
2119
using Microsoft.Azure.Commands.Profile.Models.Core;
@@ -29,12 +27,13 @@
2927
using Xunit.Abstractions;
3028
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
3129
using System;
32-
using Microsoft.Azure.Commands.ScenarioTest.Extensions;
3330
using Microsoft.Azure.Commands.Profile.Context;
3431
using System.Linq;
3532
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
3633
using Microsoft.Azure.Commands.Profile.Common;
3734
using Microsoft.Azure.Commands.ScenarioTest.Mocks;
35+
using Microsoft.Azure.Commands.TestFx.Mocks;
36+
using Microsoft.Azure.Commands.TestFx;
3837

3938
namespace Microsoft.Azure.Commands.Profile.Test
4039
{

src/Accounts/Accounts.Test/Mocks/AccountMockClientFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Commands.TestFx.Mocks;
1617
using Microsoft.Rest;
1718
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
1819
using System.Collections.Generic;
@@ -24,7 +25,7 @@ class AccountMockClientFactory : MockClientFactory, IClientFactory
2425
public delegate object NextClient();
2526

2627
private NextClient nextClient;
27-
public AccountMockClientFactory(NextClient next, bool throwIfClientNotSpecified = true):base(new List<object>(), throwIfClientNotSpecified)
28+
public AccountMockClientFactory(NextClient next, bool throwIfClientNotSpecified = true) : base(null, null)
2829
{
2930
nextClient = next;
3031
}

src/Accounts/Accounts.Test/ProfileCmdletTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.Azure.Commands.Common.Authentication.Models;
1818
using Microsoft.Azure.Commands.ScenarioTest;
19+
using Microsoft.Azure.Commands.TestFx.Mocks;
1920
using Microsoft.Azure.ServiceManagement.Common.Models;
2021
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2122
using Microsoft.WindowsAzure.Commands.ScenarioTest;

src/Accounts/Accounts.Test/SubscriptionClientSwitchTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Azure.Commands.Profile.Models;
1919
using Microsoft.Azure.Commands.Profile.Test.Mocks;
2020
using Microsoft.Azure.Commands.ScenarioTest;
21+
using Microsoft.Azure.Commands.TestFx.Mocks;
2122
using Microsoft.Azure.Management.ResourceManager.Version2021_01_01.Models;
2223
using Microsoft.Azure.ServiceManagement.Common.Models;
2324
using Microsoft.Rest.Azure;

src/Accounts/Accounts.Test/TenantCmdletMockTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.Azure.Commands.Profile;
1919
using Microsoft.Azure.Commands.Profile.Models;
2020
using Microsoft.Azure.Commands.ScenarioTest;
21+
using Microsoft.Azure.Commands.TestFx.Mocks;
2122
using Microsoft.Azure.ServiceManagement.Common.Models;
2223
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2324
using Microsoft.WindowsAzure.Commands.ScenarioTest;

0 commit comments

Comments
 (0)