Open bytinbit opened 3 years ago
This issue happens when using testbook with unittest
module as well.
I can think of two passible aproaches to mitigate this issue (without breaking backward comapatiblity)
Add a named argument in testbook
decorator which tells it to pass the notebook cleint as last argument instead of first (not a very clean approach i guess)
example:
class TestHelloWorldNoteBook(unittest.TestCase):
@testbook(notebook_path, execute=True, pass_client_in_last=True)
def test_say_hello(self, nb):
say_hello = nb.ref('say_hello')
self.assertEqual(say_hello(), 'Hello World')
Chage the decorator or create new one which acts as class decorator, and inject the client in class as class attribute example:
@testbook("foo.ipynb", execute=True) # this inject `nb` in class
class TestHelloWorldNoteBook(unittest.TestCase):
def test_say_hello(self):
say_hello = self.nb.ref('say_hello')
self.assertEqual(say_hello(), 'Hello World')
@rohitsanj
@bytinbi Here is the workaround which worked for me
Use the testbook method as context manager instead of as a decorator
import unitest
from testbook import testbook
class TestFoo(unittest.TestCase):
def test_foo(self):
with testbook(path, execute=True) as nb:
foo = nb.ref("foo")
res = foo();
self.asssertIsNotNone(res)
Altough the implementation is for unittest
but I guess it applies to pytest
as well.
If I want to group my tests using regular pytest test classes, testbook is unable to execute and fails. It might be because the
self
argument of the test class method is erroneously filled in by the fixture.Code cell to be tested (just for the record):
Test code that reproduces the error:
Error output when executing pytest + the file that contains the above code:
Without a test class, the code works perfectly fine when executing pytest + the file that contains the above code:
Output: