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

Unit tests for Inner Function without calling the outer function #307

Open
emiljoshva opened this issue Nov 15, 2023 · 1 comment
Open

Comments

@emiljoshva
Copy link

emiljoshva commented Nov 15, 2023

Outer.sh

#!/bin/bash

function Outer
{
        echo "outer"
        function Inner
        {
                echo "Inner"
        }
        Inner
}

Outer_spec.sh

Describe 'Outer'
        Include common/Outer.sh
        Describe 'Test Inner Function Call'
                It 'call Outer'
                        When call Outer
                        The output should equal "outer
Inner"
                End
                It 'call Inner'
                        When call Inner
                        The output should equal "Inner"
                End
        End
End

Upon executing
_Running: /bin/sh [bash 4.2.46(2)-release]
..F

Examples:

  1. Outer Test Inner Function Call call Inner
    When call Inner

    1.1) The output should equal Inner

       expected: "Inner"
            got: ""
    
     # spec/Outer_spec.sh:11
    

    1.2) WARNING: It exits with status non-zero but not found expectation

       status:127
    
     # spec/Outer_spec.sh:9-12
    

    1.3) WARNING: There was output to stderr but not found expectation

       stderr:/usr/local/lib/shellspec/lib/core/evaluation.sh: line 158: Inner: command not found
    
    
     # spec/Outer_spec.sh:9-12
    

Finished in 0.26 seconds (user 0.23 seconds, sys 0.05 seconds)
3 examples, 1 failure

Failure examples / Errors: (Listed here affect your suite's status)

shellspec spec/Outer_spec.sh:9 # 1) Outer Test Inner Function Call call Inner FAILED

Aborted with status code [executor: 0] [reporter: 1] [error handler: 0]
Fatal error occurred, terminated with exit status 1._

In the actual scenarios there are many inner functions and many more processing happens in the Outer(). Hence is not advisable to call Outer function everytime for an inner function.

Kindly help to write testcases for the inner functions

@alexandredosim
Copy link

alexandredosim commented Nov 15, 2023

Hello @emiljoshva. Why don't you define Inner outside Outer? With your choice, Inner is not directly testable, hence you'll need to run Outer for every Inner test. You could define a setup function and then test Inner if you really need it like this.

# Code not tested
Describe 'Inner'
  Include common/Outer.sh
  setup() {
    Outer >> /dev/null   # define Inner
  }
  It 'call Inner'
    setup
    When call Inner
    The output should equal "Inner"
  End
End

But maybe you'll need to deal with contexts (I call it context swamp of ShellSpec, the contexts are too confusing to understand at first).

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

No branches or pull requests

2 participants