How to add unit test

hey, guys, I want to use unit test, so I add aunit with alr with aunit

and this is my project

the tests/test_math.adb

with AUnit; use AUnit;
with AUnit.Test_Cases; use AUnit.Test_Cases;
with Ada.Text_IO;

package Test_Math is
   procedure TestHello;
end Test_Math;

package body Test_Math is
   package StdIO renames Ada.Text_IO;

   procedure TestHello is
   begin
      StdIO.Put_Line ("Hello World");
      Assert ( 1 = 1, "Fuck");
   end TestHello;
end Test_Math;

the tests/run_tests.adb,(this is the test entry)

with AUnit.Run;
with Test_Math;

procedure RunTests is
   procedure Run is new AUnit.Run.Test_Runner(Test_Math.TestHello);
begin
   Run;
end RunTests;

but I don’t know how to launch the tests, I have already modify the project gpr file like this

project Ada_Example is

   for Source_Dirs use ("src/", "config/", "tests/"); -- modify here, add the "tests/" 
   for Object_Dir use "obj/" & Ada_Example_Config.Build_Profile;
   for Create_Missing_Dirs use "True";
   for Exec_Dir use "bin";
   for Main use ("ada_example.adb");

   package Compiler is
      for Default_Switches ("Ada") use Ada_Example_Config.Ada_Compiler_Switches;
   end Compiler;

   package Binder is
      for Switches ("Ada") use ("-Es"); --  Symbolic traceback
   end Binder;

   package Install is
      for Artifacts (".") use ("share");
   end Install;

end Ada_Example;

and this is the toml file

name = "ada_example"
description = ""
version = "0.1.0-dev"

authors = ["steiner"]
maintainers = ["steiner <steiner3044@163.com>"]
maintainers-logins = ["steiner"]
licenses = "MIT OR Apache-2.0 WITH LLVM-exception"
website = ""
tags = []

executables = ["ada_example", "run_tests"] -- modify here, add "run_tests"

[[depends-on]]
aunit = "^25.0.0"

still cannot find the run_tests executable

steiner@macbook /V/a/w/ada_example> alr run --list                                                                       (base) 
Crate ada_example builds these executables:
   ada_example (found at /Volumes/apple-extended/workspace/ada_example/bin/ada_example)
   run_tests (not found)

I don’t know how to fix it, can you help me out ?

I use a separate binary type alire project that withs the parent project and just run the test project. So in your tests folder, create an Alire project and configure the main to run the tests

1 Like

The package file is tests/run_tests.adb but the procedure is called RunTests. Do you not get an error on that?

alreday modify the procedure name and rebuild, still the same with running alr run –list

no, I don’t get an error on that

___

Update

project Ada_Example is

   for Source_Dirs use ("src/", "config/", "tests/");
   for Object_Dir use "obj/" & Ada_Example_Config.Build_Profile;
   for Create_Missing_Dirs use "True";
   for Exec_Dir use "bin";
   for Main use ("ada_example.adb", "run_tests.adb");

   package Compiler is
      for Default_Switches ("Ada") use Ada_Example_Config.Ada_Compiler_Switches;
   end Compiler;

   package Binder is
      for Switches ("Ada") use ("-Es"); --  Symbolic traceback
   end Binder;

   package Install is
      for Artifacts (".") use ("share");
   end Install;

end Ada_Example;

just modify the gpr file like this, everything is ok now

1 Like

This might be “a” solution but using a separate crate as @sbenitezb suggested is usually the better solution in the long run as it allows you to use an unit test framework like AUnit. For details how to use it you can look at the AdaCL unit test:

AdaCL also has extensions for AUnit like specialised assertions.

Doing it with a separate crate also enables you to use alr test to start your unit tests and your test will be executed as part of the Alire continues integration pipeline. It’s all around better.

1 Like

cool, let me try it later :wink:

1 Like