Hi Bruno,
Thanks for the quick response!
Yes, I already have the code running by reading the "unittest"
documentation, but it would be good to include those changes in the Beam
documentation :)

Thanks!
Julian

On Fri, Feb 10, 2023 at 12:28 PM Bruno Volpato <bvolp...@google.com> wrote:

>
> Hi Julian,
>
> That's a good point. I think the focus of
> https://beam.apache.org/documentation/pipelines/test-your-pipeline/ is
> showing how to test Beam, but probably linking to working examples would be
> helpful to reduce friction.
>
> Related to your code, I believe you are missing 2 imports:
>
> import unittest
>
> import apache_beam as beam
>
>
> And you should invoke the test either through:
>
> python3 -m unittest test.py
>
>
> It is possible to start tests using the given command "python3 test.py
> --runner DirectRunner", but you'll need to add the following lines to your
> code:
>
> if __name__ == '__main__':
>
>   unittest.main()
>
>
>
> Best,
> Bruno
>
> On Fri, Feb 10, 2023 at 10:09 AM Julian Ogando via dev <
> dev@beam.apache.org> wrote:
>
>> Hi,
>> I'm reading the documentation found in
>> https://beam.apache.org/documentation/pipelines/test-your-pipeline/
>>
>> from apache_beam.testing.test_pipeline import TestPipelinefrom 
>> apache_beam.testing.util import assert_thatfrom apache_beam.testing.util 
>> import equal_to
>> class CountTest(unittest.TestCase):
>>
>>   def test_count(self):
>>     # Our static input data, which will make up the initial PCollection.
>>     WORDS = [
>>       "hi", "there", "hi", "hi", "sue", "bob",
>>       "hi", "sue", "", "", "ZOW", "bob", ""
>>     ]
>>     # Create a test pipeline.
>>     with TestPipeline() as p:
>>
>>       # Create an input PCollection.
>>       input = p | beam.Create(WORDS)
>>
>>       # Apply the Count transform under test.
>>       output = input | beam.combiners.Count.PerElement()
>>
>>       # Assert on the results.
>>       assert_that(
>>         output,
>>         equal_to([
>>             ("hi", 4),
>>             ("there", 1),
>>             ("sue", 2),
>>             ("bob", 2),
>>             ("", 3),
>>             ("ZOW", 1)]))
>>
>>
>> It's a little bit unclear how to run the unit test. If execute it with
>> "python3 test.py --runner DirectRunner" I get an error message:
>> class CountTest(unittest.TestCase):
>> NameError: name 'unittest' is not defined
>>
>> Thanks!
>> --
>>
>> Julian Ogando
>>
>> Cloud Data Engineer
>>
>> Latam Eng Delivery Center
>>
>> +54 911 65715933
>>
>> julianoga...@google.com
>>
>>

-- 

Julian Ogando

Cloud Data Engineer

Latam Eng Delivery Center

+54 911 65715933

julianoga...@google.com

Reply via email to