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

Run a single test by name #36

Open
mitchellh opened this issue Dec 12, 2013 · 10 comments
Open

Run a single test by name #36

mitchellh opened this issue Dec 12, 2013 · 10 comments
Milestone

Comments

@mitchellh
Copy link

All my tests take multiple minutes to run. I categorize them into separate files but it would still be nice to provide a string of a test name to only run that test to bats.

@sstephenson
Copy link
Owner

Yes, we should add this feature.

@sstephenson sstephenson added this to the 1.0 milestone Jun 2, 2014
@ahippo
Copy link
Contributor

ahippo commented Aug 19, 2014

As an extension to this pull request it may be useful to filter tests by name using a regexp or a glob-style expression.

@dougm
Copy link

dougm commented Nov 7, 2014

I'm currently using this: dougm@0b25d8a

With an Emacs mode to run the tests: https://github.com/dougm/bats-mode/blob/master/bats-mode.el

There's a key-binding to run a single bats test, wherever the cursor is currently at.

Any thoughts on the bats change above? Not sure the best way to do this, so I didn't mess with the options parser in bin/bats yet.

@ahippo
Copy link
Contributor

ahippo commented Nov 7, 2014

Looks good and working to me.
I was initially thinking of a command-line option for bats, but environment variables are ok for me too.

@dougm
Copy link

dougm commented Nov 7, 2014

@ahippo I'd like a command-line option too, but afaict the options parser in bin/bats only handles boolean options. So I just wanted to get the concept working and check if I'm on the right path before making any changes to options parser.

@ahippo
Copy link
Contributor

ahippo commented Nov 7, 2014

Yeah, you're right.
But at least adding a long option like --regex="qqq" seems pretty simple. (due to '=', it's a single argument)
Handling of --regex "qqq" or -r "qqq" requires much more changes though.

@harschware
Copy link

@dougm This seems like a very useful feature. I hope it gets added soon. It would be cool to see a feature where BATS could do test grouping similar to TestNG.

@dougm
Copy link

dougm commented Sep 2, 2015

@harschware Just opened PR #117 - I've continued to use it myself as-is via bats-mode.el since

@Sylvain303
Copy link
Contributor

Here is a simple shell script you can customize to run a single test:
$ cat one_test.sh

#!/bin/bash
batsdir=$(dirname $(readlink -f bats))
echo $batsdir
export PATH=$batsdir:$PATH

in=$1
a_test=$2

if [[ ! -e "$in" ]]
then
  echo "not found: '$in'"
  exit 2
fi

out=$(mktemp)
bats-preprocess < $in > $out

if [[ -z "$a_test" ]] || ! grep -q "^$a_test" $out
then
  echo "test '$a_test' not found in $in"
  echo "available test are:"
  grep -o -E '^test_[^ ]+' $out
  exit 1
fi

echo "temp_file: $out"
export BATS_TEST_SOURCE=$out
bats-exec-test $in $a_test

As you can see bats as already almost all for that, but you need to preprocess the file yourself, and pass the filename via export BATS_TEST_SOURCE

Not extensively tested…

list test available:

$ ./one_test.sh bats-preprocess.bats 
/home/sylvain/code/bats/test/internal
test '' not found in bats-preprocess.bats
available test are:
test_preprocess_myself()
test_dummy_test()

run one by its encoded name

$ ./one_test.sh bats-preprocess.bats test_dummy_test
/home/sylvain/code/bats/test/internal
temp_file: /tmp/tmp.qJ5hM2Rl0O
1..1
ok 1 dummy test

enjoy.

@glikson
Copy link

glikson commented Jul 3, 2017

Thanks, @Sylvain303 ! One little fix: should be "readlink -f $(which bats)" (at least it helped me run it in my env).

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

No branches or pull requests

7 participants