Search code examples
apache-beam

Beam Unit Test `assert_that` and `equal_to` doesn't actually compare the result


I noticed that the Beam assert_that and equal_to doesn't seems to be able to compare what I expected. To further testing that, I am testing with a very simple code:

import unittest

import apache_beam as beam
from apache_beam.testing.test_pipeline import TestPipeline
from apache_beam.testing.util import assert_that, equal_to


class TestBeam(unittest.TestCase):
    def test_fixed_window_grouping(self):
        with TestPipeline() as p:
            input = p | beam.Create(['a', 'b', 'c', 'd', 'e', 'f'])
        assert_that(input, equal_to([]))

I am expecting this to fail the test, however, the test pass?
Does anyone know what might went wrong with this simple test case? Thanks! (I am currently running with beam 2.41.0)


Solution

  • Your PCollection input does only exist within the context of your TestPipeline, i.e. within the with block. However, your assert_that call is done outside of the block and thus the PCollection is empty.

    This should create the desired result

    class TestBeam(unittest.TestCase):
        def test_fixed_window_grouping(self):
            with TestPipeline() as p:
                input = p | beam.Create(['a', 'b', 'c', 'd', 'e', 'f'])
                # indent the next line correctly
                assert_that(input, equal_to([]))