sagemathinc / cocalc-doc

cocalc user manaual
https://doc.cocalc.com
Creative Commons Attribution 4.0 International
19 stars 111 forks source link

add page about "How to test notebooks" #40

Open haraldschilly opened 1 year ago

haraldschilly commented 1 year ago

There are various ways how to test jupyter notebooks, especially for python and R. Document the various ways how this works on CoCalc, what's available, and at the same time make sure this actually works on CoCalc :-)

westurner commented 1 year ago

import unittest
Test = unittest.TestCase()

def test_testcase_methods_as_function_calls_that_raise_AssertionErrors():
  Test.assertTrue(True)
  Test.assertEqual("123", "123")

  with Test.assertRaises(ZeroDivisionError):
      1/0
  with Test.assertRaises(ValueError):
      int("0x0")

# a simple test runner that runs callables that start with `test_` (like pytest and iirc nose)
testscope = {**globals(), **locals()}
for name, testfunc in ((k,v) for k,v in testscope.items() if k.startswith('test_') and callable(v)):
    objstate = dict(name=name, func=testfunc, state='running')
    print(('running', objstate))
    try:
        objstate['output'] = testfunc()
        print(('completed', objstate))
    except KeyboardInterrupt:
        raise
    except Exception as exc:
        traceback.print_tb(exc.__traceback__)
        pass #