To use @unittest.skip to skip avroio_test cases when snappy is not imported.
Without snappy installed, test log would look like: WARNING:root:snappy is not installed; some tests will be skipped. ... Ran 21 tests in 13.840s OK (skipped=3) --- With installed: ... Ran 21 tests in 14.464s OK Project: http://git-wip-us.apache.org/repos/asf/beam/repo Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/c0492810 Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/c0492810 Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/c0492810 Branch: refs/heads/python-sdk Commit: c04928103932efed4bec4b6a3c710f59a5cb8469 Parents: 5b03113 Author: Younghee Kwon <[email protected]> Authored: Wed Jan 4 18:39:29 2017 -0800 Committer: Robert Bradshaw <[email protected]> Committed: Thu Jan 5 10:38:27 2017 -0800 ---------------------------------------------------------------------- sdks/python/apache_beam/io/avroio_test.py | 66 +++++++++++--------------- 1 file changed, 28 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/beam/blob/c0492810/sdks/python/apache_beam/io/avroio_test.py ---------------------------------------------------------------------- diff --git a/sdks/python/apache_beam/io/avroio_test.py b/sdks/python/apache_beam/io/avroio_test.py index db940e3..aed468d 100644 --- a/sdks/python/apache_beam/io/avroio_test.py +++ b/sdks/python/apache_beam/io/avroio_test.py @@ -21,8 +21,6 @@ import os import tempfile import unittest -import hamcrest as hc - import apache_beam as beam from apache_beam.io import avroio from apache_beam.io import filebasedsource @@ -40,6 +38,15 @@ import avro.datafile from avro.datafile import DataFileWriter from avro.io import DatumWriter import avro.schema +import hamcrest as hc + + +# Import snappy optionally; some tests will be skipped when import fails. +try: + import snappy # pylint: disable=import-error +except ImportError: + snappy = None # pylint: disable=invalid-name + logging.warning('snappy is not installed; some tests will be skipped.') class TestAvro(unittest.TestCase): @@ -256,27 +263,17 @@ class TestAvro(unittest.TestCase): expected_result = self.RECORDS self._run_avro_test(file_name, 100, True, expected_result) + @unittest.skipIf(snappy is None, 'snappy not installed.') def test_read_without_splitting_compressed_snappy(self): - try: - import snappy # pylint: disable=unused-variable - file_name = self._write_data(codec='snappy') - expected_result = self.RECORDS - self._run_avro_test(file_name, None, False, expected_result) - except ImportError: - logging.warning( - 'Skipped test_read_without_splitting_compressed_snappy since snappy ' - 'appears to not be installed.') + file_name = self._write_data(codec='snappy') + expected_result = self.RECORDS + self._run_avro_test(file_name, None, False, expected_result) + @unittest.skipIf(snappy is None, 'snappy not installed.') def test_read_with_splitting_compressed_snappy(self): - try: - import snappy # pylint: disable=unused-variable - file_name = self._write_data(codec='snappy') - expected_result = self.RECORDS - self._run_avro_test(file_name, 100, True, expected_result) - except ImportError: - logging.warning( - 'Skipped test_read_with_splitting_compressed_snappy since snappy ' - 'appears to not be installed.') + file_name = self._write_data(codec='snappy') + expected_result = self.RECORDS + self._run_avro_test(file_name, 100, True, expected_result) def test_read_without_splitting_pattern(self): pattern = self._write_pattern(3) @@ -339,25 +336,18 @@ class TestAvro(unittest.TestCase): readback = p | avroio.ReadFromAvro(path + '*') | beam.Map(json.dumps) assert_that(readback, equal_to([json.dumps(r) for r in self.RECORDS])) + @unittest.skipIf(snappy is None, 'snappy not installed.') def test_sink_transform_snappy(self): - try: - import snappy # pylint: disable=unused-variable - with tempfile.NamedTemporaryFile() as dst: - path = dst.name - with beam.Pipeline('DirectRunner') as p: - # pylint: disable=expression-not-assigned - p | beam.Create(self.RECORDS) | avroio.WriteToAvro( - path, - self.SCHEMA, - codec='snappy') - with beam.Pipeline('DirectRunner') as p: - # json used for stable sortability - readback = p | avroio.ReadFromAvro(path + '*') | beam.Map(json.dumps) - assert_that(readback, equal_to([json.dumps(r) for r in self.RECORDS])) - except ImportError: - logging.warning( - 'Skipped test_sink_transform_snappy since snappy appears to not be ' - 'installed.') + with tempfile.NamedTemporaryFile() as dst: + path = dst.name + with beam.Pipeline('DirectRunner') as p: + # pylint: disable=expression-not-assigned + p | beam.Create(self.RECORDS) | avroio.WriteToAvro( + path, self.SCHEMA, codec='snappy') + with beam.Pipeline('DirectRunner') as p: + # json used for stable sortability + readback = p | avroio.ReadFromAvro(path + '*') | beam.Map(json.dumps) + assert_that(readback, equal_to([json.dumps(r) for r in self.RECORDS])) if __name__ == '__main__':
