diff --git a/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Functional_Tests.md b/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Functional_Tests.md deleted file mode 100644 index 4dcbd0f2c..000000000 --- a/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Functional_Tests.md +++ /dev/null @@ -1,217 +0,0 @@ ---- -lab: - title: 'Setting Up and Running Functional Tests' - module: 'Module 05: Implement a secure continuous deployment using Azure Pipelines' ---- - -# Setting Up and Running Functional Tests - -## Student lab manual - -## Lab requirements - -- This lab requires **Microsoft Edge** or an [Azure DevOps supported browser.](https://docs.microsoft.com/azure/devops/server/compatibility?view=azure-devops) - -- **Set up an Azure DevOps organization:** If you don't already have an Azure DevOps organization that you can use for this lab, create one by following the instructions available at [Create an organization or project collection](https://docs.microsoft.com/azure/devops/organizations/accounts/create-organization). - -- Identify an existing Azure subscription or create a new one. - -- Verify that you have a Microsoft account or an Azure AD account with the Owner role in the Azure subscription and the Global Administrator role in the Azure AD tenant associated with the Azure subscription. For details, refer to [List Azure role assignments using the Azure portal](https://docs.microsoft.com/azure/role-based-access-control/role-assignments-list-portal) and [View and assign administrator roles in Azure Active Directory](https://docs.microsoft.com/azure/active-directory/roles/manage-roles-portal). - -## Lab overview - -[Selenium](http://www.seleniumhq.org/) is a portable open source software-testing framework for web applications. It can operate on almost every operating system. It supports all modern browsers and multiple languages, including .NET (C#) and Java. - -This lab will teach you how to execute Selenium test cases on a C# web application as part of the Azure DevOps Release pipeline. - -## Objectives - -After you complete this lab, you will be able to: - -- Configure a self-hosted Azure DevOps agent. -- Configure the release pipeline. -- Trigger build and release. -- Run tests in Chrome and Firefox. - -## Estimated timing: 60 minutes - -## Instructions - -### Exercise 0: Configure the lab prerequisites - -In this exercise, you will set up the prerequisites for the lab, which include the pre-configured Parts Unlimited team project based on an Azure DevOps Demo Generator template and Azure resources. - -#### Task 1: Configure the team project - -In this task, you will use Azure DevOps Demo Generator to generate a new project based on the **Selenium** template. - -1. On your lab computer, start a web browser and navigate to [Azure DevOps Demo Generator](https://azuredevopsdemogenerator.azurewebsites.net). This utility site will automate the process of creating a new Azure DevOps project within your account that is pre-populated with content (work items, repos, etc.) required for the lab. - - > **Note**: For more information on the site, see [What is the Azure DevOps Services Demo Generator?](https://docs.microsoft.com/azure/devops/demo-gen). - -2. Click **Sign in** and sign in using the Microsoft account associated with your Azure DevOps subscription. -3. If required, on the **Azure DevOps Demo Generator** page, click **Accept** to accept the permission requests for accessing your Azure DevOps subscription. -4. On the **Create New Project** page, in the **New Project Name** textbox, type **Setting Up and Running Functional Tests**, in the **Select organization** dropdown list, select your Azure DevOps organization, and then click **Choose template**. -5. In the list of templates, in the toolbar, click **DevOps Labs**, select the **Selenium** template and click **Select Template**. -6. Back on the **Create New Project** page, click **Create Project** - - > **Note**: Wait for the process to complete. This should take about 2 minutes. In case the process fails, navigate to your DevOps organization, delete the project, and try again. - -7. On the **Create New Project** page, click **Navigate to project**. - -#### Task 2: Create Azure resources - -In this task, you will provision an Azure VM running Windows Server 2016 along with SQL Express 2017, Chrome, and Firefox. - -1. Click here in **[Deploy to Azure](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FMicrosoft%2Falmvm%2Fmaster%2Flabs%2Fvstsextend%2Fselenium%2Farmtemplate%2Fazuredeploy.json)** link. This will automatically redirect you to the **Custom deployment** blade in the Azure portal. -2. If prompted, sign in with the user account that has the Owner role in the Azure subscription you will be using in this lab and has the role of the Global Administrator in the Azure AD tenant associated with this subscription. -3. On the **Custom deployment** blade, select **Edit template**. -4. On the **Edit template** blade, locate the line `"https://raw.githubusercontent.com/microsoft/azuredevopslabs/master/labs/vstsextend/selenium/armtemplate/chrome_firefox_VSTSagent_IIS.ps1"`, replace it with `"https://raw.githubusercontent.com/MicrosoftLearning/AZ400-DesigningandImplementingMicrosoftDevOpsSolutions/master/Allfiles/Labs/11b/chrome_firefox_VSTSagent_IIS.ps1"`, and click **Save**. -5. Back on the **Custom deployment** blade, specify the following settings: - - | Setting | Value | - | --- | --- | - | Subscription | the name of the Azure subscription you are using in this lab | - | Resource group | the name of a new resource group **az400m11l02-RG** | - | Region | the name of the Azure region in which you want to deploy the Azure resources in this lab | - | Virtual Machine Name | **az40011bvm** | - -6. Click **Review + create** and then click **Create**. - - > **Note**: Wait for the process to complete. This should take about 15 minutes. - -### Exercise 1: Implement Selenium tests by using a self-hosted Azure DevOps agent - -In this exercise, you will implement Selenium tests by using a self-hosted Azure DevOps agent. - -#### Task 1: Configure a self-hosted Azure DevOps agent - -In this task, you will configure a self-hosted agent by using the VM you deployed in the previous exercise. Selenium requires the agent to be run in the interactive mode to execute the UI tests. - -1. In the web browser window displaying the Azure portal, search for and select **Virtual machines** and, from the **Virtual machines** blade, select **az40011bvm**. -2. On the **az40011bvm** blade, select **Connect**, in the drop-down menu, select **RDP**, on the **RDP** tab of the **az40011bvm \| Connect** blade, select **Download RDP File** and open the downloaded file. -3. When prompted, sign in with the following credentials: - - | Setting | Value | - | --- | --- | - | User Name | **vmadmin** | - | Password | **P2ssw0rd@123** | - -4. Within the Remote Desktop session to **az40011bvm**, open a Chrome web browser window, navigate to **** and sign in to your Azure DevOps organization. -5. In the lower left corner of the **Azure DevOps** portal, click **Organization settings**. -6. In the vertical menu on the left hand side of the page, in the **Pipelines** section, click **Agent pools**. -7. On the **Agent pools** pane, click **Default**. -8. On the **Default** pane, click **New agent**. -9. On the **Get the agent** panel, ensure that the **Windows** tab and the **x64** section are selected and then click **Download**. -10. Start File Explorer, create a directory **C:\\AzAgent** and extract content of the downloaded agent zip file residing in the **Downloads** folder into this directory. -11. Within the Remote Desktop session to **az40011bvm**, right-click the **Start** menu and click **Command Prompt (Admin)**. -12. Within the **Administrator: Command Prompt** window, run the following to start the installation of the agent binaries: - - ```cmd - cd C:\AzAgent - Config.cmd - ``` - -13. In the **Administrator: Command Prompt** window, when prompted to **Enter server URL**, type **https://dev.azure.com/\**, where **\** represents the name of your Azure DevOps Organization, and press the **Enter** key. -14. In the **Administrator: Command Prompt** window, when prompted **Enter Authentication type (press enter for PAT)**, press the **Enter key**. -15. In the **Administrator: Command Prompt** window, when prompted **Enter personal access token**, switch to the Azure DevOps portal, close the **Get the agent** panel, in the upper right corner of the Azure DevOps page, click the **User settings** icon, in the dropdown menu, click **Personal access tokens**, on the **Personal Access Tokens** pane, and click **+ New Token**. -16. On the **Create a new personal access token** pane, specify the following settings and click **Create** (leave all others with their default values): - - | Setting | Value | - | --- | --- | - | Name | **Setting Up and Running Functional Tests lab** | - | Scopes | **Custom Defined** | - | Scopes | Click **Show all scopes** (at the bottom of the window) | - | Scopes | **Agent Pools** - **Read & Manage** | - -17. On the **Success** pane, copy the value of the personal access token to Clipboard. - - > **Note**: Make sure you copy the token. You will not be able to retrieve it once you close this pane. - -18. On the **Success** pane, click **Close**. -19. Switch back to the **Administrator: Command Prompt** window and paste the content of Clipboard and press the **Enter key**. -20. In the **Administrator: Command Prompt** window, when prompted **Enter agent pool (press enter for default)**, press the **Enter key**. -21. In the **Administrator: Command Prompt** window, when prompted **Enter agent name (press enter for az40011bvm)**, press the **Enter key**. -22. In the **Administrator: Command Prompt** window, when prompted **Enter work folder (press enter for _work)**, press the **Enter key**. -23. In the **Administrator: Command Prompt** window, when prompted **Enter run agent as service (Y/N) (press enter for N)**, press the **Enter key**. -24. In the **Administrator: Command Prompt** window, when prompted **Enter configure autologon and run agent on startup (Y/N) (press enter for N)**, press the **Enter key**. -25. Once the agent is registered, in the **Administrator: Command Prompt** window, type **run.cmd** and press the **Enter** to start the agent. - - > **Note**: You also need to install the Dac Framework which is used by the application you will be deploying later in the lab. - -26. Within the Remote Desktop session to **az40011bvm**, start another instance of the web browser, navigate to the [Microsoft SQL Server Data-Tier Application Framework (18.2) download page](https://www.microsoft.com/download/details.aspx?id=58207&WT.mc_id=rss_alldownloads_extensions) and click **Download**. -27. On the **Choose the download you want**, select the **EN\x64\DacFramework.msi** checkbox and click **Next**. This will trigger automatic download of the **DacFramework.msi** file. -28. Once the download of the **DacFramework.msi** file completes, use it to run the installation of the Microsoft SQL Server Data-Tier Application Framework with the default settings. - -#### Task 2: Configure a release pipeline - -In this task, you will configure the release pipeline. - -> **Note**: The Azure VM has the agent configured to deploy the applications and run Selenium test cases. The release definition uses **[Phases](https://docs.microsoft.com/vsts/build-release/concepts/process/phases)** to deploy to target servers. - -1. Within the Remote Desktop session to **az40011bvm**, in the browser window displaying the **Azure DevOps** portal, click the **Azure DevOps** symbol in the upper left corner. -2. On the pane displaying your organization projects, click the tile representing the **Setting Up and Running Functional Tests** project. -3. On the **Setting Up and Running Functional Tests** pane, in the vertical navigational pane, select **Pipelines**, within the **Pipelines** section, click **Releases** and then, on the **Selenium** pane, click **Edit**. -4. On the **All pipelines > Selenium** pane, click the **Tasks** tab header and, in the dropdown menu, click **Dev**. -5. Within the list of tasks of the **Dev** stage, review the **IIS Deployment**, **SQL Deployment**, and **Selenium test execution** deployment phases. - - - **IIS Deployment phase**: In this phase, we deploy application to the VM using the following tasks: - - - **IIS Web App Manage**: This task runs on the target machine where we registered agent. It creates a *website* and an *Application Pool* locally with the name **PartsUnlimited** running under the port **82** , [**http://localhost:82**](http://localhost:82) - - **IIS Web App Deploy**: This task deploys the application to the IIS server using **Web Deploy**. - - - **Database deploy phase**: In this phase, we use [**SQL Server Database Deploy**](https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/SqlDacpacDeploymentOnMachineGroup/README.md) task to deploy [**dacpac**](https://docs.microsoft.com/sql/relational-databases/data-tier-applications/data-tier-applications) file to the DB server. - - - **Selenium tests execution**: Executing **UI testing** as part of the release process allows us to detect unexpected changes. Setting up automated browser based testing drives quality in your application, without having to do it manually. In this phase, we will execute Selenium tests on the deployed web application. The subsequent tasks describe using Selenium to test the websites in the release pipeline. - - - **Visual Studio Test Platform Installer**: The [Visual Studio Test Platform Installer](https://docs.microsoft.com/azure/devops/pipelines/tasks/tool/vstest-platform-tool-installer?view=vsts) task will acquire the Microsoft test platform from nuget.org or a specified feed, and add it to the tools cache. It satisfies the **vstest** requirements so any subsequent Visual Studio Test task in a build or release pipeline can run without needing a full Visual Studio install on the agent machine. - - **Run Selenium UI tests**: This [task](https://github.com/Microsoft/azure-pipelines-tasks/blob/master/Tasks/VsTestV2/README.md) uses **vstest.console.exe** to execute the selenium test cases on the agent machines. - -6. On the **All pipelines > Selenium** pane, click the **IIS Deployment** phase and, on the **Agent job** pane, verify that the **Default** Agent pool is selected. -7. Repeat the previous step for **SQL Deployment** and the **Selenium tests execution** phases. If needed, click **Save** to save the changes. - -#### Task 3: Trigger Build and Release - -In this task, we will trigger the **Build** to compile Selenium C# scripts along with the Web application. The resulting binaries are copied to self-hosted agent and the Selenium scripts are executed as part of the automated **Release**. - -1. Within the Remote Desktop session to **az40011bvm**, in the browser window displaying the **Azure DevOps** portal, in the vertical navigational pane, in the **Pipelines** section, click **Pipelines** and then, on the **Pipelines** pane, click **Selenium**. -2. On the **Selenium** pane, click **Run pipeline** and, on the **Run pipeline**, click **Run**. - - > **Note**: This build will publish the test artifacts to Azure DevOps, which will be used in release. - - > **Note**: Once the build is successful, release will be triggered. - -3. On the pipeline runs pane, in the **Jobs** section, click **Phase 1** and monitor the build progress until its completion. -4. In the browser window displaying the **Azure DevOps** portal, in the vertical navigational pane, in the **Pipelines** section, click **Releases**, click the entry representing the release, and, on the **Selenium > Release-1** pane, click **Dev**. -5. On the **Selenium > Release-1 > Dev** pane, monitor the corresponding deployment. -6. Once the **Selenium test execution** phase starts, monitor the web browser tests. -7. Once the release completes, on the **Selenium > Release-1 > Dev** pane, click on the **Tests** tab to analyze the test results. Select the required filters from the dropdown in **Outcome** section to view the tests and their status. - -### Exercise 2: Remove the Azure lab resources - -In this exercise, you will remove the Azure resources provisioned in this lab to eliminate unexpected charges. - ->**Note**: Remember to remove any newly created Azure resources that you no longer use. Removing unused resources ensures you will not see unexpected charges. - -#### Task 1: Remove the Azure lab resources - -In this task, you will use Azure Cloud Shell to remove the Azure resources provisioned in this lab to eliminate unnecessary charges. - -1. In the Azure portal, open the **Bash** shell session within the **Cloud Shell** pane. -2. List all resource groups created throughout the labs of this module by running the following command: - - ```sh - az group list --query "[?starts_with(name,'az400m11l02-RG')].name" --output tsv - ``` - -3. Delete all resource groups you created throughout the labs of this module by running the following command: - - ```sh - az group list --query "[?starts_with(name,'az400m11l02-RG')].[name]" --output tsv | xargs -L1 bash -c 'az group delete --name $0 --no-wait --yes' - ``` - - >**Note**: The command executes asynchronously (as determined by the --nowait parameter), so while you will be able to run another Azure CLI command immediately afterwards within the same Bash session, it will take a few minutes before the resource groups are actually removed. - -## Review - -In this lab, you learned how to execute Selenium test cases on a C# web application, as part of the Azure DevOps Release pipeline. diff --git a/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Tests.md b/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Tests.md new file mode 100644 index 000000000..0c001ac0e --- /dev/null +++ b/Instructions/Labs/AZ400_M05_L09_Setting_Up_and_Running_Tests.md @@ -0,0 +1,138 @@ +--- +lab: + title: 'Setting Up and Running Tests' + module: 'Module 05: Implement a secure continuous deployment using Azure Pipelines' +--- + +# Setting Up and Running Tests + +## Student lab manual + +## Lab requirements + +- This lab requires **Microsoft Edge** or an [Azure DevOps supported browser.](https://docs.microsoft.com/azure/devops/server/compatibility) + +- **Set up an Azure DevOps organization:** If you don't already have an Azure DevOps organization that you can use for this lab, create one by following the instructions available at [Create an organization or project collection](https://learn.microsoft.com/dotnet/architecture/modern-web-apps-azure/test-asp-net-core-mvc-apps). + +## Lab overview + +Software of any complexity can fail in unexpected ways in response to changes. Thus, testing after making changes is required for all but the most trivial (or least critical) applications. Manual testing is the slowest, least reliable, most expensive way to test software. + +There are many kinds of automated tests for software applications. The simplest, lowest level test is the unit test. At a slightly higher level, there are integration tests and functional tests. Other kinds of tests, such as UI tests, load tests, stress tests, and smoke tests, are beyond the scope of this lab. + +*If you want to know more about the different types of testing, we recommend you reading this article: [Test ASP.NET Core MVC apps](https://learn.microsoft.com/dotnet/architecture/modern-web-apps-azure/test-asp-net-core-mvc-apps).* + +## Objectives + +After you complete this lab, you will be able to configure a CI pipeline for a .Net application that includes: + +- Unit Tests +- Integration Tests +- Functional Tests + +## Estimated timing: 60 minutes + +## Instructions + +### Exercise 0: Configure the lab prerequisites + +In this exercise, you will set up the prerequisites for the lab, which consist of a new Azure DevOps project with a repository based on the [eShopOnWeb](https://github.com/MicrosoftLearning/eShopOnWeb). + +#### Task 1: (skip if done) Create and configure the team project + +In this task, you will create an **eShopOnWeb** Azure DevOps project to be used by several labs. + +1. On your lab computer, in a browser window open your Azure DevOps organization. Click on **New Project**. Give your project the name **eShopOnWeb** and leave the other fields with defaults. Click on **Create**. + +#### Task 2: (skip if done) Import the eShopOnWeb Git Repository + +In this task you will import the eShopOnWeb Git repository that will be used by several labs. + +1. On your lab computer, in a browser window open your Azure DevOps organization and the previously created **eShopOnWeb** project. Click on **Repos>Files** , **Import a Repository**. Select **Import**. On the **Import a Git Repository** window, paste the following URL https://github.com/MicrosoftLearning/eShopOnWeb.git and click **Import**: + +1. The repository is organized the following way: + - **.ado** folder contains Azure DevOps YAML pipelines. + - **.devcontainer** folder container setup to develop using containers (either locally in VS Code or GitHub Codespaces). + - **.azure** folder contains Bicep & ARM infrastructure as code templates used in some lab scenarios. + - **.github** folder contains YAML GitHub workflow definitions. + - **src** folder contains the .NET website used in the lab scenarios. + +### Exercise 1: Setup Tests in CI pipeline + +In this exercise, you will setup tests in CI pipeline. + +#### Task 1: (Skip if done) Import the YAML build definition for CI + +In this task, you will add the YAML build definition that will be used to implement the Continuous Integration. + +Let's start by importing the CI pipeline named [eshoponweb-ci.yml](https://github.com/MicrosoftLearning/eShopOnWeb/blob/main/.ado/eshoponweb-ci.yml). + +1. Go to **Pipelines>Pipelines**. +1. Click on **New Pipeline** button. +1. Select **Azure Repos Git (YAML)**. +1. Select the **eShopOnWeb** repository. +1. Select **Existing Azure Pipelines YAML File**. +1. Select the **/.ado/eshoponweb-ci.yml** file then click on **Continue**. + + The CI definition consists of the following tasks: + - **DotNet Restore**: With NuGet Package Restore you can install all your project's dependency without having to store them in source control. + - **DotNet Build**: Builds a project and all of its dependencies. + - **DotNet Test**: .Net test driver used to execute unit tests. + - **DotNet Publish**: Publishes the application and its dependencies to a folder for deployment to a hosting system. In this case, it's **Build.ArtifactStagingDirectory**. + - **Publish Artifact - Website**: Publish the app artifact (created in the previous step) and make it available as a pipeline artifact. + - **Publish Artifact - Bicep**: Publish the infrastructure artifact (Bicep file) and make it available as a pipeline artifact. +1. Click the **Save** button (not **Save and run**) to save the pipeline definition. + +#### Task 2: Add tests to the CI pipeline + +In this task, you will add the integration and functional tests to the CI Pipeline. + +You can notice that the Unit Tests task is already part of the pipeline. + +- **Unit Tests** test a single part of your application's logic. One can further describe it by listing some of the things that it isn't. A unit test doesn't test how your code works with dependencies or infrastructure – that's what integration tests are for. + +1. Now you need to add the Integration Tests task after the Unit Tests task: + + ```YAML + - task: DotNetCoreCLI@2 + displayName: Integration Tests + inputs: + command: 'test' + projects: 'tests/IntegrationTests/*.csproj' + ``` + + > **Integration Tests** test how your code works with dependencies or infrastructure. Although it's a good idea to encapsulate your code that interacts with infrastructure like databases and file systems, you will still have some of that code, and you will probably want to test it. Additionally, you should verify that your code's layers interact as you expect when your application's dependencies are fully resolved. This functionality is the responsibility of integration tests. + +1. Then you need to add the Functional tests task after the Integration Tests task: + + ```YAML + - task: DotNetCoreCLI@2 + displayName: Functional Tests + inputs: + command: 'test' + projects: 'tests/FunctionalTests/*.csproj' + ``` + + > **Functional Tests** are written from the perspective of the user, and verify the correctness of the system based on its requirements. Unlike integration tests that are written from the perspective of the developer, to verify that some components of the system work correctly together. + +16. Click **Save**, on the **Save** pane, click **Save** again to commit the changes directly into the main branch. + +#### Task 4: Check the tests summary + +1. Click on the **Run**, then from the **Run pipeline** tab, click on **Run** again. + +1. Wait for the pipeline to start and until it completes the Build Stage successfully. + +1. Once completed, the **Test** tab will show as part of the pipeline run. Click on it to check the summary. It looks like shown below: + + ![Tests Summary](images/AZ400_M05_L09_Tests_Summary.png) + +1. For more details, at the bottom of the page, the table shows a list of the different run tests. + + >**Note**: If the table is empty, you need to reset the filters to have all the details about the tests run. + + ![Tests Table](images/AZ400_M05_L09_Tests_Table.png) + +## Review + +In this lab, you learned how to setup and run different tests types using Azure Pipelines and .Net. diff --git a/Instructions/Labs/images/AZ400_M05_L09_Tests_Summary.png b/Instructions/Labs/images/AZ400_M05_L09_Tests_Summary.png new file mode 100644 index 000000000..e9e46aa9d Binary files /dev/null and b/Instructions/Labs/images/AZ400_M05_L09_Tests_Summary.png differ diff --git a/Instructions/Labs/images/AZ400_M05_L09_Tests_Table.png b/Instructions/Labs/images/AZ400_M05_L09_Tests_Table.png new file mode 100644 index 000000000..28bf5b57f Binary files /dev/null and b/Instructions/Labs/images/AZ400_M05_L09_Tests_Table.png differ