Skip to content

Latest commit

 

History

History
171 lines (125 loc) · 5.81 KB

math_module_testing.livemd

File metadata and controls

171 lines (125 loc) · 5.81 KB

Math Module Testing

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Math Module Testing

Generally, we can split up our test suite into test cases. Each test case may require many assertions.

For this exercises, we're going to test a Math module that abstracts the operators for adding different data types together, like so.

Math.add(4, 4) # 
Math.add("a", "b") # "ab"
Math.add([1], [2]) # [1, 2]

Our Happy-path test cases (where our code is used as expected) could be the following.

Math.add/2

  • add two integers
  • add two strings
  • add two lists

Math.subtract/2

  • subtract two integers
  • subtract two strings
  • subtract two lists

We also want to consider edge-case test cases, also called sad-path or unhappy path when things go wrong, or the code is misused. For example, we might consider the following cases.

Math.add/2

  • add a valid data type (integer, string, list) and an invalid data type.
  • add two invalid data types.
  • add two empty lists.
  • add two empty strings.
  • add a string by an empty string.
  • add a list by an empty list.

Math.subtract/2

  • subtract a valid data type (integer, string, list) and an invalid data type.
  • subtract two invalid data types.
  • subtract two empty lists.
  • subtract two empty strings.
  • subtract a string by an empty string.
  • subtract a list by an empty list.

There can be a deceptive number of edge cases to consider. For example, we could build a growing list of edge-case permutations for each data type we want the Math module to handle.

Here, we've colored happy path tests green and edge-case tests yellow.

By planning test cases, we can anticipate possible edge cases and ensure we understand the desired behavior of the feature.

Test and implement a Math module. Include at least two assertions for each happy path test case (strings, lists, and integers.)

defmodule Math do
end

ExUnit.start(auto_run: false)

defmodule MathTest do
  use ExUnit.Case

  # Tests Go Here
end

ExUnit.run()

Bonus: Edge Cases

Decide how to handle calling the Math.add/2 and Math.subtract/2 functions with invalid data. For example, you might raise a FunctionClauseError using guards.

Note that if you expect to raise an error, it's usually idiomatic to name our functions using a bang ! symbol, so Math.add/2 and Math.subtract/2 should be renamed to Math.add!/2 and Math.subtract!/2.

Math.add!(1, 1)
2

Math.add!(%{}, %{})
** (FunctionClauseError) no function clause matching in Math.add/2  

Alternatively or in addition, you might choose to change the return value to an {:ok, value} tuple or {:error, error} tuple.

Math.add(%{}, %{})
{:error, :invalid_data}

Test and implement these edge cases on your Math module above.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Math Module Testing exercise"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation