bot_army v1.0.0 BotArmy.IntegrationTest View Source

Adds macros to assist in running bot trees in ExUnit.

Create a test file as per the normal ExUnit process, for example, in test/my_project_test.exs:

defmodule MyProjectTest do
  @moduledoc false

  # This will set up ExUnit.Case, import `action` from BotArmy.Actions and
  # alias BehaviorTree.Node.  You can pass `async: true` just like with `use
  # ExUnit.Case` if you want to run this test file in parallel to other async
  # files (be careful of tests that mutate a global state!).
  use BotArmy.IntegrationTest, async: true

  alias MyProject.Actions.Sample

  # use this if you want a log file (applies to all tests in module)
  log_to_file()

  # use this if you use a custom bot module (applies to all tests in module)
  use_bot_module(MyProject.CustomBot)

  # normal ExUnit setup how ever you need
  setup do
    %{magic_number: 9}
  end

  # you can also setup/cleanup via a tree similar to using `test_tree`
  pre_all_tree "pre all" do
    Node.sequence([action(BotArmy.Actions, :log, ["Pre all ..."])])
  end

  # Run test trees with `test_tree`, which works very much like ExUnit's
  # `test`, except it will run the bot and pass or fail based on its outcome.
  #
  # Setting the `:verbose` tag will show all of the bot's logs for that test,
  # otherwise only errors will show.

  @tag :verbose
  test_tree "validate target number", context do
    Node.select([
      action(Sample, :validate_number, [context.magic_number]),
      action(BotArmy.Actions, :error, [context.magic_number <> " is an invalid number"])
    ])
  end

Note, if a tree takes longer than 1 minute to run, it will fail the test. You can set @moduletag timeout: n (or @tag timeout: n per test) to raise this limit.

Link to this section Summary

Functions

Include log_to_file() if you want the full logs to be saved to a file. You can pass a path to the file you want to log to, or it defaults to bot_run.log.

Runs a tree after all the tests in the module.

Runs a tree after each test in the module.

Runs a tree before all the tests in the module.

Runs a tree before each test in the module.

Runs a bot tree. Used internally, but you could call it directly if you have a reason to.

This works very much like ExUnit's test macro, except it runs your bot tree and fails or succeeds based on the outcome. The body must return a valid tree. You can use values from the context in the tree, just as with normal ExUnit tests.

Specify this at the top of your module to use a specific bot module (see BotArmy.Bot).

Link to this section Functions

Link to this macro

log_to_file(path \\ "bot_run.log") View Source (macro)

Include log_to_file() if you want the full logs to be saved to a file. You can pass a path to the file you want to log to, or it defaults to bot_run.log.

Link to this macro

post_all_tree(message, context \\ quote do _ end, list) View Source (macro)

Runs a tree after all the tests in the module.

The body must return a tree.

Link to this macro

post_tree(message, context \\ quote do _ end, list) View Source (macro)

Runs a tree after each test in the module.

The body either needs to return a tree or nil, which is useful if you want to conditionally run a tree based on a test's tag.

Link to this macro

pre_all_tree(message, context \\ quote do _ end, list) View Source (macro)

Runs a tree before all the tests in the module.

The body must return a tree.

Link to this macro

pre_tree(message, context \\ quote do _ end, list) View Source (macro)

Runs a tree before each test in the module.

The body either needs to return a tree or nil, which is useful if you want to conditionally run a tree based on a test's tag.

Link to this function

run_tree(tree, bot_id, opts \\ []) View Source

Runs a bot tree. Used internally, but you could call it directly if you have a reason to.

Takes an opts param that can include bot_module (defaults to BotArmy.Bot.Default).

Link to this macro

test_tree(message, context \\ quote do _ end, contents) View Source (macro)

This works very much like ExUnit's test macro, except it runs your bot tree and fails or succeeds based on the outcome. The body must return a valid tree. You can use values from the context in the tree, just as with normal ExUnit tests.

Your tree will be wrapped in order to always call the done action when it finishes (to prevent the default of looping through the tree from the top).

Link to this macro

use_bot_module(mod) View Source (macro)

Specify this at the top of your module to use a specific bot module (see BotArmy.Bot).

Defaults to BotArmy.Bot.Default.