Skip to content

Commit

Permalink
Add section about operating system specific tests (#7755)
Browse files Browse the repository at this point in the history
* add section about operating system specific tests

* Update content/doc/developer/testing/index.adoc

Co-authored-by: Zbynek Konecny <[email protected]>

* Apply suggestions from code review

Co-authored-by: Mark Waite <[email protected]>

---------

Co-authored-by: Zbynek Konecny <[email protected]>
Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2024
1 parent a3108f7 commit c9bb3d5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions content/doc/developer/testing/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,51 @@ To recreate the same configuration from a test method, you can do the following:
In order to test parts of your plugin, you may want certain files to exist in the build workspace, or that Jenkins is configured in a certain way.
This section covers various ways to achieve this using the Jenkins Test Harness.

=== Operating System Awareness

Sometimes it is necessary to adjust a test depending on the operating system.
This can be the choice of Batch or Shell commands or other test conditions that are operating system specific.
The following example uses `Functions.isWindows()` to create different code depending on the operating system:

[source,java]
----
import hudson.Functions;
import hudson.model.FreeStyleProject;
import hudson.tasks.BatchFile;
import hudson.tasks.Shell;
import org.jvnet.hudson.test.JenkinsRule;
import org.junit.Rule;
import org.junit.Test;
public class MyPluginTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Test
public void testOnAllSystems() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
if (Functions.isWindows()) {
project.getBuildersList().add(new BatchFile("echo ahoy > log.log"));
} else {
project.getBuildersList().add(new Shell("echo ahoy > log.log"));
}
j.buildAndAssertSuccess(project);
}
}
----

Sometimes a test should be skipped because it is only relevant for one type of operating system or because it needs specific conditions that depend on the test environment.
Tests can be skipped based on conditionals evaluated by link:https://www.baeldung.com/junit-conditional-assume[JUnit Assume].
Use `assumeFalse()` to disable a test on Windows systems in combination with `Functions.isWindows()`:

`assumeFalse("TODO: Implement this test on Windows", Functions.isWindows());`

The test will be skipped on Windows.

Tests run on all platforms by default.
Jenkins encourages testing on multiple operating systems, including Windows.

==== Customizing the Build Workspace


Expand Down

0 comments on commit c9bb3d5

Please sign in to comment.