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

Example on how to test a function #233

Closed
pmusset opened this issue Dec 5, 2023 · 6 comments
Closed

Example on how to test a function #233

pmusset opened this issue Dec 5, 2023 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@pmusset
Copy link

pmusset commented Dec 5, 2023

In the examples here, I think it misses one that shows how to test a specific function in a script.

For example, in a directory organised like that, as describe in your quickstart guide,

.
├── lib
│   └── bashunit
├── example.sh
└── tests
    └── example_test.sh

I am not sure what I have to write in example_test.sh to test a function in example.sh. Should I source example.sh ? But according to your own tests (for example here) it doesn't seem to be the case.

@pmusset pmusset added the enhancement New feature or request label Dec 5, 2023
@antonio-gg-dev
Copy link
Member

Hello @pmusset ! First of all, thank you very much for using bashunit!

You are correct, you should add source example.sh at the beginning of your example_test.sh, personally, I recommend you do it within the set_up function.

function set_up() {
  source example.sh
}

This way, all the functions within the file will be loaded from scratch before each test inside that file is executed.

In the example you mentioned, we are not using source because bashunit uses itself for testing, so it doesn't need to import its own functions as they have already been imported previously in the runtime.

But the standard is to have to do it.

I hope I have resolved your doubts and if you have any other questions do not hesitate to let us know.

@antonio-gg-dev
Copy link
Member

I also wanted to thank you for giving us feedback on the example @pmusset . You are absolutely right. I will add it as soon as possible, but if you want, you can do it yourself and make us a PR, we would really appreciate it a lot.

@antonio-gg-dev antonio-gg-dev self-assigned this Dec 5, 2023
@antonio-gg-dev antonio-gg-dev added documentation Improvements or additions to documentation question Further information is requested and removed enhancement New feature or request labels Dec 5, 2023
@antonio-gg-dev antonio-gg-dev moved this to Todo in bashunit Dec 5, 2023
@Chemaclass
Copy link
Member

Thanks for your feedback, @pmusset. I created this PR to split the example into two: one to test bash functions and another to test an executable bash script. Does this helps?

@pmusset
Copy link
Author

pmusset commented Dec 6, 2023

Thanks to both of you. Now I understand better.

So that also means that the file that is sourced shouldn't contain any code that will execute when it is sourced, right ?

@Chemaclass
Copy link
Member

@pmusset, that would depend on the way you are designing your logic.
There is no right or wrong here, and for either way, you can have tests for it.

My personal preference:

  1. I prefer to have isolated functions and test them from an isolated perspective for small/unit tests.
  2. And then, also some more generic tests which need to be "sourcing" the whole file as that's how you execute a bash script in the end.

@Chemaclass
Copy link
Member

Closed as the question was answered and the example code was improved here #234

@github-project-automation github-project-automation bot moved this from Todo to Done in bashunit Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: Done
Development

No branches or pull requests

3 participants