Project: Beam (App Definition and Development / Streaming & Messaging)
File: test-your-pipeline.md

title: "Test Your Pipeline" <!-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> {{< toc >}} Testing your pipeline is a particularly important step in developing an effective data processing solution. The indirect nature of the Beam model, in which your user code constructs a pipeline graph to be executed remotely, can make debugging failed runs a non-trivial task. Often it is faster and simpler to perform local unit testing on your pipeline code than to debug a pipeline's remote execution. Before running your pipeline on the runner of your choice, unit testing your pipeline code locally is often the best way to identify and fix bugs in your pipeline code. Unit testing your pipeline locally also allows you to use your familiar/favorite local debugging tools. You can use , a local runner helpful for testing and local development. After you test your pipeline using the `DirectRunner`, you can use the runner of your choice to test on a small scale. For example, use the Flink runner with a local or remote Flink cluster. The Beam SDKs provide a number of ways to unit test your pipeline code, from the lowest to the highest levels. From the lowest to the highest level, these are: You can test the individual functions used in your pipeline. You can test an entire as a unit. You can perform an end-to-end test for an entire pipeline. To support unit testing, the Beam SDK for Java provides a number of test classes in the . You can use these tests as references and guides. To test a transform you've created, you can use the following pattern: Create a `TestPipeline`. Create some static, known test input data. Use the `Create` transform to create a `PCollection` of your input data. `Apply` your transform to the input `PCollection` and save the resulting output `PCollection`. Use `PAssert` and its subclasses to verify that the output `PCollection` contains the elements that you expect. {{< paragraph class="language-java" >}} is a class included in the Beam Java SDK specifically for testing transforms. {{< /paragraph >}} {{< paragraph class="language-py" >}} is a class included in the Beam Python SDK specifically for testing transforms. {{< /paragraph >}} For tests, use `TestPipeline` in place of `Pipeline` when you create the pipeline object. Unlike `Pipeline.create`,

handles setting `PipelineOptions` internally. You create a `TestPipeline` as follows: {{< highlight java >}} Pipeline p = TestPipeline.create(); {{< /highlight >}} {{< highlight py >}} with TestPipeline as p: ... {{< /highlight >}} Note: Read about testing unbounded pipelines in Beam in . You can use the `Create` transform to create a `PCollection` out of a standard in-memory collection class, such as Java or Python `List`. See for more information. is a class included in the Beam Java SDK that is an assertion on the contents of a `PCollection`. You can use `PAssert`to verify that a `PCollection` contains a specific set of expected elements. For a given `PCollection`, you can use `PAssert` to verify the contents as follows: {{< highlight java >}} PCollection<String> output = ...; // Check whether a PCollection contains some elements in any order. PAssert.that(output) .containsInAnyOrder( "elem1", "elem3", "elem2"); {{< /highlight >}} {{< highlight py >}} from apachebeam.testing.util import assertthat from apachebeam.testing.util import equalto output = ... assert_that( output, equal_to(["elem1", "elem3", "elem2"])) {{< /highlight >}} {{< paragraph class="language-java" >}} Any Java code that uses `PAssert` must link in `JUnit` and `Hamcrest`. If you're using Maven, you can link in `Hamcrest` by adding the following dependency to your project's `pom.xml` file: {{< /paragraph >}} {{< highlight java >}} <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest</artifactId> <version>2.2</version> <scope>test</scope> </dependency> {{< /highlight >}} For more information on how these classes work, see the package documentation. The following code shows a complete test for a composite transform. The test applies the `Count` transform to an input `PCollection` of `String` elements. The test uses the `Create` transform to create the input `PCollection` from a `List<String>`. {{< highlight java >}} public class CountTest { // Our static input data, which will make up the initial PCollection. static final String[] WORDS_ARRAY = new String[] { "hi", "there", "hi", "hi", "sue", "bob", "hi", "sue", "", "", "ZOW", "bob", ""}; static final List<String> WORDS = Arrays.asList(WORDS_ARRAY); public void testCount() { // Create a test pipeline. Pipeline p = TestPipeline.create(); // Create an input PCollection. PCollection<String> input = p.apply(Create.of(WORDS)); // Apply the Count transform under test. PCollection<KV<String, Long>> output = input.apply(Count.<String>perElement()); // Assert on the results. PAssert.that(output) .containsInAnyOrder( KV.of("hi", 4L), KV.of("there", 1L), KV.of("sue", 2L), KV.of("bob", 2L), KV.of("", 3L), KV.of("ZOW", 1L)); // Run the pipeline. p.run(); } } {{< /highlight >}} {{< highlight py >}} import unittest import apache_beam as beam from apachebeam.testing.testpipeline import TestPipeline from apachebeam.testing.util import assertthat from apachebeam.testing.util import equalto class CountTest(unittest.TestCase): def test_count(self): WORDS = [ "hi", "there", "hi", "hi", "sue", "bob", "hi", "sue", "", "", "ZOW", "bob", "" ] with TestPipeline() as p: input = p | beam.Create(WORDS) output = input |

assert_that( output, equal_to([ ("hi", 4), ("there", 1), ("sue", 2), ("bob", 2), ("", 3), ("ZOW", 1)])) {{< /highlight >}} You can use the test classes in the Beam SDKs (such as `TestPipeline` and `PAssert` in the Beam SDK for Java) to test an entire pipeline end-to-end. Typically, to test an entire pipeline, you do the following: For every source of input data to your pipeline, create some known static test input data. Create some static test output data that matches what you expect in your pipeline's final output `PCollection`(s). Create a `TestPipeline` in place of the standard `Pipeline.create`. In place of your pipeline's `Read` transform(s), use the `Create` transform to create one or more `PCollection`s from your static input data. Apply your pipeline's transforms. In place of your pipeline's `Write` transform(s), use `PAssert` to verify that the contents of the final `PCollection`s your pipeline produces match the expected values in your static output data. The following example code shows how one might test the . `WordCount` usually reads lines from a text file for input data; instead, the test creates a `List<String>` containing some text lines and uses a `Create` transform to create an initial `PCollection`. `WordCount`'s final transform (from the composite transform `CountWords`) produces a `PCollection<String>` of formatted word counts suitable for printing. Rather than write that `PCollection` to an output text file, our test pipeline uses `PAssert` to verify that the elements of the `PCollection` match those of a static `String` array containing our expected output data. {{< highlight java >}} public class WordCountTest { // Our static input data, which will comprise the initial PCollection. static final String[] WORDS_ARRAY = new String[] { "hi there", "hi", "hi sue bob", "hi sue", "", "bob hi"}; static final List<String> WORDS = Arrays.asList(WORDS_ARRAY); // Our static output data, which is the expected data that the final PCollection must match. static final String[] COUNTS_ARRAY = new String[] { "hi: 5", "there: 1", "sue: 2", "bob: 2"}; // Example test that tests the pipeline's transforms. public void testCountWords() throws Exception { Pipeline p = TestPipeline.create(); // Create a PCollection from the WORDS static input data. PCollection<String> input = p.apply(Create.of(WORDS)); // Run ALL the pipeline's transforms (in this case, the CountWords composite transform). PCollection<String> output = input.apply(new CountWords()); // Assert that the output PCollection matches the COUNTS_ARRAY known static output data. PAssert.that(output).containsInAnyOrder(COUNTS_ARRAY); // Run the pipeline. p.run(); } } {{< /highlight >}} {{< highlight py >}} import unittest import apache_beam as beam from apachebeam.testing.testpipeline import TestPipeline from apachebeam.testing.util import assertthat from apachebeam.testing.util import equalto class CountWords(beam.PTransform): class WordCountTest(unittest.TestCase): WORDS = [ "hi", "there", "hi", "hi", "sue", "bob", "hi", "sue", "", "", "ZOW", "bob", "" ] EXPECTED_COUNTS = ["hi: 5", "there: 1", "sue: 2", "bob: 2"] def testcountwords(self): with TestPipeline() as p: input = p | beam.Create(WORDS) output = input | CountWords() assertthat(output, equalto(EXPECTED_COUNTS), label='CheckOutput') {{< /highlight >}}