CommonTest help

Apologies for this but…

can anybody point me where to go to get some help with commontest under rebar3 …I have no hair left. I have read the official documentation until it now just looks like colored pixels on my monitor I can absorb no more. I’ve tried google groups, the trap_exit site looks dead ( years since I looked). I don’t know where to turn :frowning:

I have never used ct before today… TBH either I am so dumb I don’t get it or it’s way too hard to get working, I’ve read the epgsql code again and again. I want a test suite to be able to drop and recreate a test database.

All I want is for my “application under test” to be able to “application:get_env/2” to work… it’s driving me freaking nuts. I had a bowel cancer op in March, I may well have to quite software engineering as this level of stress isn’t good for me I’m sure.

Thanks
Sean

application:get_env/2 should work, can you perhaps share a minimal project that shows your problem?

PS: Remember that you need to make sure that the application is loaded before you can read its documentation. I’m not sure if CT did that by default.

Hi Nobbz,
I don’t think I can really…I have a “dbms” module that calls application:get_env/2 and the results is always undefined except when running normally from rebar shell.

I have been driven to the edge of the maelstrom by commontest today to be honest.

Ironically even running under rebar eunit is doing the same thing now…must have got out of bed the wrong way or something.

foo_test() ->
    Cfg = application:get_env(scofblog, dbms),
    ?assertNotMatch(Cfg, undefined).

running under rebar3 shell however it’s fine

1> application:get_env(scofblog, dbms).
{ok,[{server,"localhost"},
     {database,"scofblog"},
     {username,"sean"},
     {password,"???????"},
     {blogroot,"/home/sean/Documents/code/haskell/monadic-warrior-hakyll-site/posts"},
     {blogspec,"*.md"}]}

and he is my rebar.config file for good measure

{erl_opts, [debug_info]}.
{cover_enabled, true}.

{deps, [
    {epgsql,   {git, "https://github.com/epgsql/epgsql.git"},     {tag, "4.4.0"}},
    {cowboy,   {git, "https://github.com/ninenines/cowboy.git"},  {tag, "2.9.1"}},
    {jsone,    {git, "https://github.com/sile/jsone.git"},        {tag, "v0.3.3"}},
    {gen_smtp, {git, "https://github.com/gen-smtp/gen_smtp.git"}, {tag, "0.15.0"}}
]}.

{profiles,
 [
  {test,
   [
    {deps, [meck]}
   ]}
]}.


{shell, [
  % {config, "config/sys.config"},                                                             
    {apps, [scofblog]}
]}.

might as well throw in the failed suite as well

-module(dbms_SUITE).


-export([all/0,
         init_per_suite/1,
         init_per_testcase/2,
         end_per_suite/1,
         end_per_testcase/2
        ]).

-export([can_insert_row/1, can_insert_new_topic/1]).


-include("./src/scofblog_common.hrl").



all() -> [can_insert_new_topic, can_insert_row].


init_per_suite(_InitConfig) ->
    %% there MUST be a "rebar3" way of doing this, I read the epgsql
    %% test code for ages but really I just need to get this project
    %% finished for now!
    {ok, [{application, scofblog, Props}]}
        = file:consult("../../../../src/scofblog.app.src"),

    Env = ?prop(env, Props),
    Dbms = ?prop(dbms, Env),

    %% independant database connection
    {ok, C} =
        epgsql:connect(
          ?prop(server, Dbms),
          ?prop(username, Dbms),
          ?prop(password, Dbms),
          #{database=> "scofblog_test"}
         ),
    ct:pal("DBMS connected: ~w~n", [C]),
    [{conn, C}].


end_per_suite(Config) ->
    Conn = ?prop(conn, Config),
    epgsql:close(Conn),
    ct:pal("end_per_suite: DB Closeed OK~n",[]).


%% TEST init / end
init_per_testcase(Test, Config) ->
    ct:pal("init_per_testcase: for ~s~n", [Test]),
    C = ?prop(conn, Config),
    ct:pal("init_per_testcase: C: ~w~n", [C]),

    %T1 = epgsql:squery(C, "truncate comment cascade"),
    %T2 = epgsql:squery(C, "truncate author cascade"),
    %T3 = epgsql:squery(C, "truncate topic cascade"),

    %ct:pal("T1: ~w~n", [T1]),
    %ct:pal("T2: ~w~n", [T2]),
    %ct:pal("T3: ~w~n", [T3]),
    Config.

end_per_testcase(Test, _Config) ->
    ct:pal("end_per_testcase: ~s~n", [Test]).


can_insert_new_topic(_Cfg) ->
    {ok, TC} = test_connect(),
    epgsql:close(TC).


can_insert_row(Config) ->
    C = ?prop(conn, Config),
    ct:pal("TEST: can_insert_row: config: ~w~n", [C]),
    1 =:= 2.


test_connect() ->
    dbms:connect("scofblog_test").

What do you see when start common test, and how do you start it? Do you see the output from your calls to ct:pall? Try adding an extra one to the start of init_per_suite/1 function. What is shown in the generated HTML files?

I do see the print-and-log output in the HTML files, that’s not a problem. I just can’t figure out why I can’t load my application:get_env() without so much trouble!

It’s been a few years since I used Erlang and EUnit but I don’t remember having this trouble before, stuff just worked like it said it should.

I’ve probably done something fundamentally borked but these things never transpire to reveal themselves until one has walked at least three lengths of the Nakatome building in bare feet…

Nobbz, I have realised that the application isn’t being loaded but I thought that at least running “rebar3 eunit” would do that for me but it doesn’t look that way.

I’ll be honest, I have found reading the manuals regarding EUnit and COmmonTest to be absolutely fruitless when trying to find a plain simple example of what I need: ensuring that my
src/scofblog.app.src is to hand at runtime.

That’s one thing I do remember, that the manuals are not the best sources of beginner level examples, I googled for about three hours on and off, found plenty of “solutions” but they are all years old and didn’t work anyway.

Still reading, gone back to EUnit manual and taking it one step at a time.
Again.
:expressionless:

Hello, sorry for the delay, went for a three hour walk to defuse my brain. Sore feet but clam head now.

I start with rebar3 ct or rebar3 eunit and no extra command line options.

Ultimately I lack the flying hours with the tools I guess. I have always loved Erlang since I first used it almost a decade ago and I do remember it being one tough cookie to crumble at times

:slight_smile:

I have done most of my common test using the ct commands from the shell so I am not that into using rebar3. Can you check which applications are loaded and started. rebar3 shell does it for you automatically but I don’t know how the testing works.

I woke early with my head full of stuff… anyway, I think I have solved my problem by the simple addition of application:ensure_all_started, making my new working init_per_suite like so:

    application:ensure_all_started(scofblog),  
    {ok, Dbms} = application:get_env(scofblog, dbms),

    {ok, C} =
        epgsql:connect(
          ?prop(server, Dbms),
          ?prop(username, Dbms),
          ?prop(password, Dbms),
          #{database=> "scofblog_test"}
         ),
    [{conn, C}].

I found some code written by a work buddy from my current job, we use Erlang for talking to and from RabbitMQ and also to provide a common library that the Elixir applications use to “get on the bus”…lucky for me there was a -single- SUITE test that had that line in it, kind of obvious once you see it but not yesterday!

So, Robert, thanks for the nudges… a tad gutted I didn’t solve it myself but hell, at least I am moving forwards again, thanks again to everybody that helped a sometimes overly woud-up aging hacker… :expressionless:

I am still looking for a “solid” and “reliable” way to drop/recreate a test database, currently I create it manually and just truncate the tables to save some time…

Sean Charles.