Undefined function when running common tests

Hello, could anybody point out some mistakes I am doing or give some guidance on why my function is undefined when running the common tests.

I have the following file /src/config_handler.erl

-module(config_handler).

-export([election_delay/0]).

election_delay() ->
    application:get_env(elector, election_delay, 3000).

My test file is /test/config_handler_SUITE.erl

-module(config_handler_SUITE).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
-export([groups/0, all/0, init_per_group/2, end_per_group/2]).
-export([election_delay_test/1]).

groups() ->
    [{config_handler_group, [], [election_delay_test]}].

all() ->
    [{group, config_handler_group}].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, _Config) ->
    application:set_env(elector, election_delay, 3000).

election_delay_test(_Config) ->
    application:set_env(elector, election_delay, 5000),
    ?assert(config_handler:election_delay() == 5000).

When running the test with: ct_run -dir test -logdir test_logs

I get:
=== === Reason: undefined function config_handler:election_delay/0
in function config_handler_SUITE:election_delay_test/1 (config_handler_SUITE.erl, line 21)
in call from test_server:ts_tc/3 (test_server.erl, line 1782)
in call from test_server:run_test_case_eval1/6 (test_server.erl, line 1291)
in call from test_server:run_test_case_eval/9 (test_server.erl, line 1223)

I am not sure why the function is undefined although it is defined and exported. Does anybody else have any ideas?
Thanks in advance :slight_smile: I recently started coding in erlang so I might be doing some stupid mistakes that I am not aware of.

2 Likes

The problem is that module config_handler is not loaded.

ct_run will run a new Erlang VM and by default it doesn’t know where your compiled code is located. Try adding flag -pa <path_to_directory_contatining_.beam_files>, e.g. ct_run -dir test -pa ./ebin. This way, compiled code that is placed on the specified path will be loaded by the VM on startup and CT can access it.

Try reading this and try few examples from this to get familiar with CT.

3 Likes

Working now when added the -pa <path_to_directory_contatining_.beam_files>to the ct_run command. Thanks!

3 Likes