bash - BATS: Make variable persistent across all tests -


i'm writing bats (bash automated testing system) script , i'd variable persisted across tests. example:

#!/usr/bin/env bats  # generate random port number port_num=$(shuf -i 2000-65000 -n 1)  @test "test number one" {     = $port_num }  @test "test number two" {     b = $port_num } 

when evaluated, , b should equal each other. doesn't work, though, because (according docs) entire file evaluated after each test run. means $port_num gets regenerated between tests. there way/place me store variables persisted across tests?

export environmental variable.

# if env var $port_num doesn't exist, set it. if [ -z "$port_num" ];     export port_num=$(shuf -i 2000-65000 -n 1) fi 

in bats must call load source file.

put above code in file named port.bash in directory executing from.

then before functions, call load port. set $port_num once, , not change it.

load port  @test "test number one" {     = $port_num }  @test "test number two" {     b = $port_num } 

Comments