Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic test registration #349

Conversation

martin-schulze-vireso
Copy link
Member

@martin-schulze-vireso martin-schulze-vireso commented Sep 11, 2020

I played a bit around with the framework to see if we could get parametrized tests working, so this is more of a proof of concept.
This needs #348 but I wanted to make those separate so we can play around here.

I see the following issues with this PR:

  1. it adds another layer of execution to the test files, so we now run them n+2 times
  2. with bats-exec-suite sourceing the test file, there is even more collision surface between tests and the framework, this becomes especially nasty when there are syntax errors in the test files

Closes #306

@lucastheisen
Copy link

If i understand the code correctly, this basically allows you to define functions:

function assert_equals {
  if [[ "$1" != "$2" ]]; then
    echo "[$1] != [$2]"
  fi
}

Which can be run at any point later by invoking a another function:

bats_test_function --description "one is one" assert_equals "one" "one"
bats_test_function --description "two is two" assert_equals "two" "two"
bats_test_function --description "two is three" assert_equals "two" "three"

Then you would see a single set of results:

 ✓ one is one
 ✓ two is two
 ✗ two is three
   (from function `assert_equals' in file tests.bats, line ?)
     `?' failed

3 tests, 1 failure

This seems sufficiently dynamic for most use cases.

When you say

there is even more collision surface between tests and the framework

is that just to say that bats will be less helpful in pointing out where your syntax errors are? or that things might not actually work because your tests have vars that collide with bats internals?

@martin-schulze-vireso
Copy link
Member Author

When you say

there is even more collision surface between tests and the framework

is that just to say that bats will be less helpful in pointing out where your syntax errors are? or that things might not actually work because your tests have vars that collide with bats internals?

The main problem is: when we hook code into bats, it might mess with internal variables. Additionally, if there is a syntax error in the sourced file, bats will go down with it, potentially swallowing the error (or giving unhelpful filenames due to the preprocessing). I am thinking about jailing this source into an extra bash invocation to avoid this.

@martin-schulze-vireso martin-schulze-vireso added this to the v2.0.0 milestone Apr 25, 2021
@martin-schulze-vireso martin-schulze-vireso modified the milestones: v2.0.0, 1.11.0 Jul 16, 2023
fnordahl added a commit to fnordahl/microovn that referenced this pull request Aug 10, 2023
Add a test case that incrementally scales up a cluster from 1 to
4 nodes, (re-)running the cluster tests after each scaling event.

To work around the fact that bats-core does currently not provide
any way of programmatically composing tests, we use the workaround
provided in bats-core/bats-core#241.  Essentially we use a bats
test definition file as a wrapper to invoke tests from a different
bats test definition file.

Note that the outer bats runner will perform validation on the
number of tests ran based on TAP output, so it is important that
the inner bats runner uses a different format for its output.

We should monitor the progress of bats-core/bats-core#349 and move
to something like that when it materializes.

Signed-off-by: Frode Nordahl <frode.nordahl@canonical.com>
@martin-schulze-vireso martin-schulze-vireso force-pushed the feature/dynamic_test_registration branch 2 times, most recently from 9e00e86 to 8f27554 Compare December 25, 2023 23:47
This ensure we detect when a change disables Bats from reporting failures.
This would not be detectable by Bats' selftests.
- simplify stack trace limiting
due to unbound variable BATS_BEGIN_CODE_QUOTE
@martin-schulze-vireso
Copy link
Member Author

Any thoughts in this @brokenpip3 ?

Copy link
Contributor

@brokenpip3 brokenpip3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💪

@martin-schulze-vireso martin-schulze-vireso changed the title WIP: dynamic test registration dynamic test registration Jan 26, 2024
@martin-schulze-vireso martin-schulze-vireso merged commit 4f56098 into bats-core:master Jan 26, 2024
54 checks passed
Copy link
Contributor

@debarshiray debarshiray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I happened to notice something while reading the code:

_bats_test_functions_setup -1 # invalid TEST_NUMBER, as this is not a test

BATS_TEST_NAME=source
BATS_TEST_FILTER="$BATS_TEST_FILTER" bats_preprocess_source # uses BATS_TEST_FILENAME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't seem to be anything left that's using BATS_TEST_FILTER.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to remove BATS_TEST_FILTER in #907

Copy link
Contributor

@debarshiray debarshiray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke test suites with multiple files loading common constants. I have tried to put together a possible fix with test cases to illustrate the problem in #904

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[wishlist] dynamic test registration
4 participants