nteract / testbook

🧪 📗 Unit test your Jupyter Notebooks the right way
https://testbook.readthedocs.io
BSD 3-Clause "New" or "Revised" License
416 stars 37 forks source link

Return value as None from testbook function #143

Closed ranjiGT closed 2 years ago

ranjiGT commented 2 years ago

I am writing a code for auto-grading my notebook based on the test_stdout() example. It successfully checks the conditions as defined by tb.cell_output_text() but when I perform some basic operations the return value is None is what I get, instead of the variable that I pass into this function.

Note: The below I call from a external python file to execute my notebook.

from testbook import testbook

@testbook('./ssnn_solved.ipynb', execute=True)
def test_stdout(tb):
    cells = [7, 13, 17, 21]
    score = 0
    for cell in cells:
        if tb.cell_output_text(cell) == 'Test passed.':
            score += 1
    return score

res = test_stdout()
print(res)

Outputs : None

But desired: 4

rohitsanj commented 2 years ago

Hi @ranjiGT! Thanks for raising this bug! This was a simple fix, and I'll be raising a PR soon. :)

rohitsanj commented 2 years ago

Meanwhile, you could consider using the with testbook(..) as tb pattern instead of the decorator pattern.

from testbook import testbook

def test_stdout(tb):
    cells = [7, 13, 17, 21]
    score = 0
    with testbook('./ssnn_solved.ipynb', execute=True):
        for cell in cells:
            if tb.cell_output_text(cell) == 'Test passed.':
                score += 1
        return score

res = test_stdout()
print(res)
ranjiGT commented 2 years ago

Hi @rohitsanj, it now works

I believe the code should be :

from testbook import testbook

def test_stdout():
    cells = [7, 13, 17, 21]
    score = 0
    with testbook('./ssnn_solved.ipynb', execute=True) as tb:
        for cell in cells:
            if tb.cell_output_text(cell) == 'Test passed.':
                score += 1
        return score

res = test_stdout()
print(res)

Because def test_stdout(tb) takes tb as object and gives TypeError: test_stdout() missing 1 required positional argument: 'tb'

Thanks!

rohitsanj commented 2 years ago

Yep, that's right! My bad :)