bashunit: Tests failing when sourcing files with execution outside of functions
Hello,
I am trying out your framework with some files I possess, but for some reason, every time I run my tests after sourcing my script.sh file, my tests fail. For context, my script.sh file is sourcing another file (outside of any function) and also exports some variables (outside of any function). I am wondering if this is a bug in the framework or if the framework does not support sourcing files that execute commands outside of functions for testing.
Thank you.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (11 by maintainers)
Btw: I also experienced the problem that output on
stderrmakes test fail.The biggest DX issue with this IMO is, that we don’t get a clear error message which shows why the test is failling (e.g. because of output to stderr on line xy)
Hello, thank you for your help: Basically, I downloaded the bashunit framework using the following command: curl -s https://bashunit.typeddevs.com/install.sh | bash, which gives me the newest version, and added it onto a folder called bashunit_tests where I have my test.sh file. I added the external.sh and script.sh files in a different directory and the test seems to pass if:
However this framework is a bit confusing since if I try to do this in my script.sh
with external.sh having:
my test.sh passes:

but if I remove # echo “$(dirname)” from script.sh, my test fails
Sorry if this seems confusing I am just trying to debug as much as I can
Thank you for answering here’s a snippet:
external.sh
#!/bin/bashexport MY_VARIABLE="Hello, World!"echo "Sourced external.sh"script.sh:
#!/bin/bashsource ./external.shhelp() {echo "Usage: script.sh [options]"exit 1 }test.sh
function set_up() {source 'script.sh' }function test_help() {result="$(help)"assert_exit_code "1" }The test.sh is run through ./bashunit test.sh as mentioned in the documentation on the website
When I do that the assertion is passing but the test itself is failing because of having source, echo, and export being outside of functions. The tests pass normally if I remove all three but my problem is I have more lengthy files with a lot more sourcing and export commands so I wanted to make sure the framework is compatible before moving forward