|
| 1 | +# Run MATLAB Tests on Jenkins Server |
| 2 | + |
| 3 | +This example shows you how to run a suite of MATLAB® unit tests with Jenkins™. The example enables you to: |
| 4 | + |
| 5 | +* Configure Jenkins to run a freestyle project including MATLAB tests. |
| 6 | +* Interface Jenkins with a remote repository that contains the tests. |
| 7 | +* Build the freestyle project and examine the generated test artifacts. |
| 8 | + |
| 9 | +The project will run the tests in the *Times Table App* MATLAB project (requires R2019a or later). You can create a working copy of the project files and open the project in MATLAB by running the following statement in the Command Window. |
| 10 | + |
| 11 | +``` |
| 12 | +matlab.project.example.timesTable |
| 13 | +``` |
| 14 | + |
| 15 | +For more information about the *Times Table App* example project, see [Explore an Example Project](https://www.mathworks.com/help/matlab/matlab_prog/explore-an-example-project.html). |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | +Running the tests in this example requires you to interface Jenkins with MATLAB as well as a remote repository. |
| 19 | + |
| 20 | +* To run MATLAB tests and generate test artifacts, you must install the Jenkins plugin for MATLAB. For information on how to install a plugin in Jenkins, see [Managing Plugins](https://jenkins.io/doc/book/managing/plugins/). |
| 21 | +* Jenkins runs the tests in a branch of the remote repository that is specified by a URL. To follow the build steps in this example, the *Times Table App* project must be under Git™ source control. For example, you can create a new repository for the project using your GitHub® account. For more information, see [Use Source Control with Projects](https://www.mathworks.com/help/matlab/matlab_prog/use-source-control-with-projects.html). |
| 22 | +* To publish the test artifacts using post-build actions, relevant plugins must be installed on Jenkins. For example, the [Cobertura plugin](https://plugins.jenkins.io/cobertura) integrates Cobertura coverage reports to Jenkins, and [JUnit plugin](https://plugins.jenkins.io/junit) enables JUnit-format test results to be published. |
| 23 | + |
| 24 | +## Configure Jenkins Project to Run MATLAB Tests |
| 25 | +Create a new project and configure it by following these steps: |
| 26 | +1. In your Jenkins interface, select **New Item** on the left. A new page opens where you can choose the type of your project. Enter a project name, then click **Freestyle project**. To confirm your choices, click **OK**. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +2. In the configuration window of your Jenkins project, navigate to the **Source Code Management** section and click **Git**. This option enables Jenkins to interface with a remote repository. |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +3. Navigate to the main page of the GitHub repository that hosts your tests. Click **Clone or download** and copy the web URL to your clipboard. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +4. In the Jenkins interface, paste the URL into the **Repository URL** field of the **Source Code Management** section. (You also can specify the branch to build in the **Branch Specifier** field.) |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +5. In the **Build** section of Jenkins, click **Add build step > Run MATLAB Tests**. A new window opens that enables you to configure the Jenkins plugin for MATLAB. Paste the full path to the MATLAB root folder into the **MATLAB root** field; choose the default Automatic test mode from the **Test mode** drop-down list; and select your desired test artifacts to be generated in the project workspace. (The plugin in this example is configured to generate Cobertura code coverage and JUnit test result reports.) For more information on how to configure the Jenkins plugin for MATLAB, see [Jenkins MATLAB Plugin](https://github.com/jenkinsci/matlab-plugin). |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +6. In the **Post-build Actions** section of Jenkins, add two post-build actions to publish the Cobertura code coverage and JUnit test result reports that are generated. For each report, provide the path to the report file. Jenkins stores the artifacts in the *matlabTestArtifacts* folder of the workspace. |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +7. Click **Save** to save the project configuration settings. You can access and modify your settings at a later stage by selecting **Configure** in the project interface. |
| 51 | + |
| 52 | +## Run Tests and Inspect Test Artifacts |
| 53 | +To build your Jenkins project and run the tests specified in the repository, click **Build Now** in the project interface, which displays the project name at the top-left of the page. Jenkins triggers a build, assigns it a number under **Build History**, and runs the build. If the build is successful, a blue circle icon appears next to the build number. If the build fails, Jenkins adds a red circle icon. In this example, the build passes because all of the tests specified in the *Times Table App* project pass. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +Navigate to the project workspace by clicking the **Workspace** icon in the project interface. You can view the generated test artifacts in the *matlabTestArtifacts* folder of the workspace. |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +Access the published Cobertura code coverage report by opening the *Coverage Report* link in the project interface. |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +To view the published JUnit test results, open the *Latest Test Result* link in the project interface. In the new page, open the link in the *All Tests* table. The table expands and lists information for each of the test classes within the *Times Table App* project. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +## See Also |
| 74 | +[MathWorks Blogs: Developer Zone – Continuous Integration](https://blogs.mathworks.com/developer/category/continuous-integration/)<br/> |
| 75 | +[matlab.unittest.plugins Package](https://www.mathworks.com/help/matlab/ref/matlab.unittest.plugins-package.html)<br/> |
| 76 | +[Explore an Example Project (MATLAB)](https://www.mathworks.com/help/matlab/matlab_prog/explore-an-example-project.html)<br/> |
| 77 | +[Use Source Control with Projects (MATLAB)](https://www.mathworks.com/help/matlab/matlab_prog/use-source-control-with-projects.html) |
0 commit comments