From aeffa5d1a6dd66de91eaca9d082f6379fa0aa360 Mon Sep 17 00:00:00 2001 From: Harshil Valecha Date: Sat, 28 Nov 2026 11:37:28 +0530 Subject: [PATCH] Port CombinePerKeyExamplesTest off DoFnTester Run ExtractLargeWordsFn and FormatShakespeareOutputFn in a TestPipeline and check their output with PAssert, like BigQueryTornadoesTest. The assertions now check the exact output instead of only that it contains each expected element. The PTransform style guide still recommended DoFnTester for unit testing DoFns; point it at TestPipeline and PAssert instead. Fixes #41727 --- .../cookbook/CombinePerKeyExamplesTest.java | 37 ++++++++++--------- .../en/contribute/ptransform-style-guide.md | 2 +- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/examples/java/src/test/java/org/apache/beam/examples/cookbook/CombinePerKeyExamplesTest.java b/examples/java/src/test/java/org/apache/beam/examples/cookbook/CombinePerKeyExamplesTest.java index cd19bf993684..30a113997a49 100644 --- a/examples/java/src/test/java/org/apache/beam/examples/cookbook/CombinePerKeyExamplesTest.java +++ b/examples/java/src/test/java/org/apache/beam/examples/cookbook/CombinePerKeyExamplesTest.java @@ -17,15 +17,17 @@ */ package org.apache.beam.examples.cookbook; -import static org.hamcrest.MatcherAssert.assertThat; - import com.google.api.services.bigquery.model.TableRow; -import java.util.List; +import java.util.Arrays; import org.apache.beam.examples.cookbook.CombinePerKeyExamples.ExtractLargeWordsFn; import org.apache.beam.examples.cookbook.CombinePerKeyExamples.FormatShakespeareOutputFn; -import org.apache.beam.sdk.transforms.DoFnTester; +import org.apache.beam.sdk.testing.PAssert; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.sdk.transforms.Create; +import org.apache.beam.sdk.transforms.ParDo; import org.apache.beam.sdk.values.KV; -import org.hamcrest.CoreMatchers; +import org.apache.beam.sdk.values.PCollection; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -33,6 +35,7 @@ /** Unit tests for {@link CombinePerKeyExamples}. */ @RunWith(JUnit4.class) public class CombinePerKeyExamplesTest { + @Rule public TestPipeline p = TestPipeline.create(); private static final TableRow row1 = new TableRow().set("corpus", "king_lear").set("word", "snuffleupaguses"); @@ -66,21 +69,19 @@ public class CombinePerKeyExamplesTest { .set("all_plays", "king_lear,macbeth"); @Test - public void testExtractLargeWordsFn() throws Exception { - DoFnTester> extractLargeWordsFn = - DoFnTester.of(new ExtractLargeWordsFn()); - List> results = extractLargeWordsFn.processBundle(ROWS_ARRAY); - assertThat(results, CoreMatchers.hasItem(tuple1)); - assertThat(results, CoreMatchers.hasItem(tuple2)); - assertThat(results, CoreMatchers.hasItem(tuple3)); + public void testExtractLargeWordsFn() { + PCollection> results = + p.apply(Create.of(Arrays.asList(ROWS_ARRAY))).apply(ParDo.of(new ExtractLargeWordsFn())); + PAssert.that(results).containsInAnyOrder(tuple1, tuple2, tuple3); + p.run().waitUntilFinish(); } @Test - public void testFormatShakespeareOutputFn() throws Exception { - DoFnTester, TableRow> formatShakespeareOutputFn = - DoFnTester.of(new FormatShakespeareOutputFn()); - List results = formatShakespeareOutputFn.processBundle(COMBINED_TUPLES_ARRAY); - assertThat(results, CoreMatchers.hasItem(resultRow1)); - assertThat(results, CoreMatchers.hasItem(resultRow2)); + public void testFormatShakespeareOutputFn() { + PCollection results = + p.apply(Create.of(Arrays.asList(COMBINED_TUPLES_ARRAY))) + .apply(ParDo.of(new FormatShakespeareOutputFn())); + PAssert.that(results).containsInAnyOrder(resultRow1, resultRow2); + p.run().waitUntilFinish(); } } diff --git a/website/www/site/content/en/contribute/ptransform-style-guide.md b/website/www/site/content/en/contribute/ptransform-style-guide.md index 050a4bf93265..fa1ba93cd961 100644 --- a/website/www/site/content/en/contribute/ptransform-style-guide.md +++ b/website/www/site/content/en/contribute/ptransform-style-guide.md @@ -175,7 +175,7 @@ Data processing is tricky, full of corner cases, and difficult to debug, because * Leaks of `Closeable`/`AutoCloseable` resources in failure cases * Common corner cases when developing sources: complicated arithmetic in `BoundedSource.split` (e.g. splitting key or offset ranges), iteration over empty data sources or composite data sources that have some empty components. * Mock out the interactions with third-party systems, or better, use ["fake"](https://martinfowler.com/articles/mocksArentStubs.html) implementations when available. Make sure that the mocked-out interactions are representative of all interesting cases of the actual behavior of these systems. -* To unit test `DoFn`s, `CombineFn`s, and `BoundedSource`s, consider using `DoFnTester`, `CombineFnTester`, and `SourceTestUtils` respectively which can exercise the code in non-trivial ways to flesh out potential bugs. +* To unit test `DoFn`s, run them in a `TestPipeline` and check their output with `PAssert`. For `CombineFn`s and `BoundedSource`s, consider using `CombineFnTester` and `SourceTestUtils` respectively, which can exercise the code in non-trivial ways to flesh out potential bugs. * For transforms that work over unbounded collections, test their behavior in the presence of late or out-of-order data using `TestStream`. * Tests must pass 100% of the time, including in hostile, CPU- or network-constrained environments (continuous integration servers). Never put timing-dependent code (e.g. sleeps) into tests. Experience shows that no reasonable amount of sleeping is enough - code can be suspended for more than several seconds. * For detailed instructions on test code organization, see the [Beam Testing Guide](https://cwiki.apache.org/confluence/display/BEAM/Contribution+Testing+Guide).