Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Easy way to run just one test #164

Closed
bcv opened this issue May 9, 2016 · 4 comments
Closed

Easy way to run just one test #164

bcv opened this issue May 9, 2016 · 4 comments

Comments

@bcv
Copy link

bcv commented May 9, 2016

Hi

Is there a way I can specify just to run one test ?
I have written around 42 tests of which only one is failing. To fix, I have to just run the 41st test. I end up running all other 41 before it.
I can add skip to each and every test to skip them but this is combersome and while checking-in the bats test script into a version control system I have to remove the skip commands

Is there a way to say
bats -t filename testnumber
or something in those lines ?

@ztombol
Copy link

ztombol commented May 9, 2016

Unfortunately Bats can't do this. Yet!

A similar feature, running only tests whose description matches a pattern, has been proposed in #36 and #117. But due to the lack of free time of the maintainer it was never merged.

Being able to run tests by their number would be incredibly useful. Especially since the TAP format output (--tap), used under CI systems by default, readily shows the test numbers.

Manpower has been gathering in #150 to continue development towards 1.0. I've been going through the issue tracker as well as my own list of suggestions and been writing up a list of changes I will suggest for the next release. This feature has been at the top my list already!

This feature may take time to bake once development restarts, but thinking of its usefulness I think it will definitely arrive sooner or later. Until then see the workaround below!

Stay tuned!

Workaround

Since setup is run before each test, you can use BATS_TEST_NUMBER there to skip all but the test you need. This does not eliminate the work of cleaning up before commiting but is siginificantly easier to add and remove than individual skip statements in all tests.

#!/usr/bin/env bats

run_only_test() {
  if [ "$BATS_TEST_NUMBER" -ne "$1" ]; then
    skip
  fi
}

setup() {
  run_only_test 2
}

@test 'test #1' {
  true
}

@test 'test #2' {
  true
}

@test 'test #3' {
  true
}

When run, it will only run the second test and skip all others.

$ bats test.bats 
 - test #1 (skipped)
 ✓ test #2
 - test #3 (skipped)

3 tests, 0 failures, 2 skipped

Note: BATS_TEST_NUMBER is only available in setup, @test and teardown. Outside these functions, e.g. in global scope, this workaround does not work.

@bcv
Copy link
Author

bcv commented May 10, 2016

if [ "$BATS_TEST_NUMBER" -ne "$1" ]; then
skip
fi

That is brilliant solution. Accepting this as solution for now.

@ztombol
Copy link

ztombol commented May 10, 2016

@bcv Glad it works for you!

One more thing. BATS_TEST_NUMBER is only available in setup, @test and teardown (and any functions called by them). Outside of any of these functions, e.g. in global scope, this variable is not set.

@bcv bcv closed this as completed May 12, 2016
@ztombol ztombol mentioned this issue Dec 13, 2016
18 tasks
@robatwilliams
Copy link

Might be more convenient to use the test description than the number:

testOnly() {
    if [ "$BATS_TEST_DESCRIPTION" != "$1" ]; then
        skip
    fi
}

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

No branches or pull requests

3 participants