Test cells in Week 2 Assignment: how to construct?


I found the test cells in Week2 's programming assignment very helpful and I want to apply this trick to my other programs.
How are the test cells constructed? Thank you.

Hi @Bill_Zou, these test cells are in-built by the programmers or the staff concerned. However, if you would want to create a new cell within the assignment to test the cases then you would have to go to the tab ‘Insert’—> then create the cell above or below as per your requirement and then test the cases. Does this justify your query? Thanks!

1 Like

As @Rashmi says, it’s easy to insert new cells in a notebook: just use “Insert → Cell Below”. You can see that they use two types of tests in the notebooks here:

  1. Ones where they literally write out the test code in the notebook, so you can see both the code and the expected results.
  2. Ones where they call an external function like sigmoid_test in the example you gave. For those, you can find the source by clicking “File → Open” and then opening the file public_tests.py. Sometimes you also notice that they have another file with test “utility” routines that they call from the public_tests.py functions.

You can learn how they do it in public_tests.py and either copy that style or invent your own. You could create your own external “dot py” file if you want and call those functions from the notebook, but I think it’s probably easier just to write your own directly in the notebook. You just have to be careful not to include anything in your added cells that looks like a “grader tag” (comments that include strings the grader looks for like UNQ_C1). Of course, you also need to be respectful of the “name space”: don’t redefine the names of any global objects defined in the given code in the notebook.

1 Like