Testing on functions that have results that may change

I am new and this feels like a stupid question so please just hear me out. I have a project that will work its way through a directory and find any video files in non-video folders and visa versa. I want to put some testing in but I have never done serious testing before. How would I go about it? And wouldn’t testing be impossible since the result could change when I send it to my client or whenever any of the folders I am using for testing change? Or is this just a problem where I have to just deal with it?

Here is the GitHub link

It sounds like you are thinking that you should be testing against “production” data. This is not the case. In testing, you assert against some fixed, deterministic data that you setup.

For example, based on a very quick look at your code, you could set up the following directory structure to test against:

test/
  test_dirs/
    dir1/
      file1.txt
      file2.txt
      file3.mov
    dir2/
      file1.txt
      file2.txt

Then in your tests you would do something like:

defmodule ErovfTest do
  use ExUnit.Case

  describe "main_function/1" do
    test "finds video files" do
      dir = "test/test_dirs/dir1"

      files = main_function("test/support/dir1")

      assert files == ["file3.mov"]
    end

    test "finds no video files" do
      dir = "test/test_dirs/dir2"

      files = main_function(dir)

      assert Enum.empty?(files)
    end
  end
end
5 Likes

You could extract side effect functions to the boundary and keep the Core functional.

For example, separate the dir browsing from the rest.

And in the core, pass a list of files, or a file as param of the function.

4 Likes

Thank you so much for the feedback guys! I appreciate the example a ton sodapopcan. I do have a couple of questions when it comes to verbiage for kokolegorille, what is a side effect function? What do you mean by boundary and core?

1 Like

A pure function always return the same result for the same input.

A side effect function does not…

Boundaries are the layers close to the outside world.

Core is the name I give to my most inner layer, composed with pure functions only :slight_smile:

You can learn more about it here…

2 Likes

For this one the __DIR__ special form can be your friend. You can root the directory of files to whatever directory your test is in, which will make your code organization better than if you root it to a subdirectory of test/

3 Likes

Absolutely. As soon as I posted this I realized how I was referencing the directories was a little sloppy.

In practice, for this type of thing, I would do what @kokolegorille is suggesting. In generally, I wouldn’t want to have to set up directories of files for testing but rather pass data structures representing the filesystem to the functional core. I wanted to keep the example simple and inline with the repo provided, though.

What is the __ DIR __ special form? Sorry for all the questions guys you’ve been amazing and I appreciate you all.

Kernel.SpecialForms — Elixir v1.12.3DIR/0

Lol the dunders don’t work in hyperlinks… Anyways you can find it on that page

1 Like

This link should work: Kernel.__DIR__/0