<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: What libraries could be used for unit testing of the Spark code? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/what-libraries-could-be-used-for-unit-testing-of-the-spark-code/m-p/22841#M15721</link>
    <description>&lt;P&gt;There are several libraries for Scala and Python that help with writing unit tests for Spark code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For Scala you can use following:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Built-in Spark test suite - it's designed  to test all parts of Spark. It supports RDD, Dataframe/Dataset, Streaming APIs &lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/holdenk/spark-testing-base" alt="https://github.com/holdenk/spark-testing-base" target="_blank"&gt;spark-testing-base&lt;/A&gt; - supports both Scala &amp;amp; Python. Supports RDD, Dataframe/Dataset, and Streaming APIs. &lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/MrPowers/spark-fast-tests" alt="https://github.com/MrPowers/spark-fast-tests" target="_blank"&gt;spark-fast-tests&lt;/A&gt; - supports both Spark 2 &amp;amp; 3, very simple, easy to use API. Easy to combine with Scalatest&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For Python you can use also following (in addition to the spark-testing-base)&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://github.com/MrPowers/chispa" alt="https://github.com/MrPowers/chispa" target="_blank"&gt;chispa&lt;/A&gt; - Python port of spark-fast-tests&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/malexer/pytest-spark" alt="https://github.com/malexer/pytest-spark" target="_blank"&gt;pytest-spark&lt;/A&gt; - simplifies Spark session creation, natively integrated with pytest&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code examples for all of this libraries could be found in &lt;A href="https://github.com/alexott/spark-playground/tree/master/testing" alt="https://github.com/alexott/spark-playground/tree/master/testing" target="_blank"&gt;one place&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is small example of using Chispa:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from chispa import *
import pyspark.sql.functions as F
from pyspark.sql import SparkSession
&amp;nbsp;
&amp;nbsp;
spark = SparkSession.builder.master("local").appName("chispa").getOrCreate()
&amp;nbsp;
&amp;nbsp;
def remove_non_word_characters(col):
    return F.regexp_replace(col, "[^\\w\\s]+", "")
&amp;nbsp;
&amp;nbsp;
def test_removes_non_word_characters_short():
    data = [("jo&amp;amp;&amp;amp;se", "jose"), ("**li**", "li"), ("#::luisa", "luisa"), (None, None)]
    df = spark.createDataFrame(data, ["name", "expected_name"]).withColumn(
        "clean_name", remove_non_word_characters(F.col("name"))
    )
    assert_column_equality(df, "clean_name", "expected_name")
&amp;nbsp;
&amp;nbsp;
def test_remove_non_word_characters_nice_error():
    data = [("matt7", "matt"), ("bill&amp;amp;", "bill"), ("isabela*", "isabela"), (None, None)]
    df = spark.createDataFrame(data, ["name", "expected_name"]).withColumn(
        "clean_name", remove_non_word_characters(F.col("name"))
    )
    assert_column_equality(df, "clean_name", "expected_name")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Jun 2021 10:01:50 GMT</pubDate>
    <dc:creator>alexott</dc:creator>
    <dc:date>2021-06-18T10:01:50Z</dc:date>
    <item>
      <title>What libraries could be used for unit testing of the Spark code?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-libraries-could-be-used-for-unit-testing-of-the-spark-code/m-p/22840#M15720</link>
      <description>&lt;P&gt;We need to add unit test cases for our code that we're writing using the Scala in Python.  But we can't use the calls like `assertEqual` for comparing the content of DataFrames. Are any special libraries for that?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jun 2021 09:54:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-libraries-could-be-used-for-unit-testing-of-the-spark-code/m-p/22840#M15720</guid>
      <dc:creator>alexott</dc:creator>
      <dc:date>2021-06-18T09:54:12Z</dc:date>
    </item>
    <item>
      <title>Re: What libraries could be used for unit testing of the Spark code?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-libraries-could-be-used-for-unit-testing-of-the-spark-code/m-p/22841#M15721</link>
      <description>&lt;P&gt;There are several libraries for Scala and Python that help with writing unit tests for Spark code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For Scala you can use following:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Built-in Spark test suite - it's designed  to test all parts of Spark. It supports RDD, Dataframe/Dataset, Streaming APIs &lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/holdenk/spark-testing-base" alt="https://github.com/holdenk/spark-testing-base" target="_blank"&gt;spark-testing-base&lt;/A&gt; - supports both Scala &amp;amp; Python. Supports RDD, Dataframe/Dataset, and Streaming APIs. &lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/MrPowers/spark-fast-tests" alt="https://github.com/MrPowers/spark-fast-tests" target="_blank"&gt;spark-fast-tests&lt;/A&gt; - supports both Spark 2 &amp;amp; 3, very simple, easy to use API. Easy to combine with Scalatest&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For Python you can use also following (in addition to the spark-testing-base)&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://github.com/MrPowers/chispa" alt="https://github.com/MrPowers/chispa" target="_blank"&gt;chispa&lt;/A&gt; - Python port of spark-fast-tests&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/malexer/pytest-spark" alt="https://github.com/malexer/pytest-spark" target="_blank"&gt;pytest-spark&lt;/A&gt; - simplifies Spark session creation, natively integrated with pytest&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code examples for all of this libraries could be found in &lt;A href="https://github.com/alexott/spark-playground/tree/master/testing" alt="https://github.com/alexott/spark-playground/tree/master/testing" target="_blank"&gt;one place&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is small example of using Chispa:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from chispa import *
import pyspark.sql.functions as F
from pyspark.sql import SparkSession
&amp;nbsp;
&amp;nbsp;
spark = SparkSession.builder.master("local").appName("chispa").getOrCreate()
&amp;nbsp;
&amp;nbsp;
def remove_non_word_characters(col):
    return F.regexp_replace(col, "[^\\w\\s]+", "")
&amp;nbsp;
&amp;nbsp;
def test_removes_non_word_characters_short():
    data = [("jo&amp;amp;&amp;amp;se", "jose"), ("**li**", "li"), ("#::luisa", "luisa"), (None, None)]
    df = spark.createDataFrame(data, ["name", "expected_name"]).withColumn(
        "clean_name", remove_non_word_characters(F.col("name"))
    )
    assert_column_equality(df, "clean_name", "expected_name")
&amp;nbsp;
&amp;nbsp;
def test_remove_non_word_characters_nice_error():
    data = [("matt7", "matt"), ("bill&amp;amp;", "bill"), ("isabela*", "isabela"), (None, None)]
    df = spark.createDataFrame(data, ["name", "expected_name"]).withColumn(
        "clean_name", remove_non_word_characters(F.col("name"))
    )
    assert_column_equality(df, "clean_name", "expected_name")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Jun 2021 10:01:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-libraries-could-be-used-for-unit-testing-of-the-spark-code/m-p/22841#M15721</guid>
      <dc:creator>alexott</dc:creator>
      <dc:date>2021-06-18T10:01:50Z</dc:date>
    </item>
  </channel>
</rss>

