# Test Erlang behavior with focused examples

Source: https://zotonic.com/docs/2984/test-erlang-behavior-with-focused-examples

URI: https://zotonic.com/id/2984

Language: en

Keep domain logic in small functions that accept explicit inputs. Test expected results, boundary values, and meaningful error cases without starting a full site when the function does not need one.

For code that uses Zotonic models, use the repository's existing test setup and a dedicated site context. Clean up created resources so later tests do not depend on execution order.

Select a known test module with the supported test runner. Check that the runner actually discovers it: this workspace's `runtests` command scans the core `apps/zotonic_*/test` directories, not every user application's test directory.

For the pure filter example, create `test/filter_garden_label_tests.erl` in the Garden application:

```erlang
-module(filter_garden_label_tests).
-include_lib("eunit/include/eunit.hrl").

label_test() ->
    ?assertEqual(<<"Open to visitors">>,
        filter_garden_label:garden_label(<<"open">>, undefined)),
    ?assertEqual(<<"Check visiting times">>,
        filter_garden_label:garden_label(undefined, undefined)).
```

After building the application, compile this test into a temporary directory and run it with the application's `ebin` on the path:

```sh
mkdir -p /tmp/garden-eunit
erlc -o /tmp/garden-eunit apps_user/zotonic_mod_garden/test/filter_garden_label_tests.erl
erl -noshell -pa _build/default/lib/zotonic_mod_garden/ebin /tmp/garden-eunit -eval 'case eunit:test(filter_garden_label_tests, [verbose]) of ok -> halt(0); _ -> halt(1) end.'
```

Expect one passing EUnit test. These commands run a pure helper test without a site; use the site's integration test setup for database or permission behaviour.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2984?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2984?language=en>)
