Wolfram Research

Function Repository Resource:

OpenTestWritingPalette (1.0.0) current version: 2.0.0 »

Source Notebook

Create a palette with tools for creating unit tests

Contributed by: Sjoerd Smit

ResourceFunction["OpenTestWritingPalette"][]

creates a palette with buttons that allow you to create VerificationTest expressions from pairs of input-output cells.

ResourceFunction["OpenTestWritingPalette"][fun]

uses fun[] to create a TestID for each test that is created.

ResourceFunction["OpenTestWritingPalette"][None]

creates tests without a TestID.

Details

You can select multiple input-output cell groups to generate tests in batches.
You can also create VerificationTest expressions from tests in testing notebooks.
Copy tests wil copy a plain-text string to the clipboard that you can easily paste into a .wlt file or anywhere else.
Create tests will print a new cell with tests underneath the selected cells.
Convert to tests will delete the selected cells and replace them with a cell with all the tests.
Add tests to file will write tests to a plain text file. To use this, first select an output file using the Browse… button.
Write selected cells to file will read the contents of one or more selected cells as plain text and add them to directly to the output file. This is essentially a copy-paste shortcut.

Examples

Basic Examples (3) 

Open the palette:

In[1]:=
ResourceFunction["OpenTestWritingPalette"][]
Out[1]=

The palette has a number of buttons:

The buttons under the heading "Creating tests" all do similar things: they generate tests from input-output cell groups. Evaluate some input to test and then select the CellGroup and press Create tests to create a VerificationTest that can be run immediately:

In[2]:=
1 + 1
Out[2]=
In[3]:=
VerificationTest[
 	1 + 1
 	,
 	2 ,
 	TestID -> "Test-2f26924a-3e99-47bb-8686-f7bd391d786f"
 ]
Out[3]=

If you use the Browse… button, you can select a plain-text output file to directly append new tests to.

Scope (3) 

Generated messages will be added to the test as well:

In[4]:=
1/0
Out[4]=
In[5]:=
VerificationTest[
 	1/0
 	,
 	ComplexInfinity
 	,
 	{Power::infy}
 	,
 	TestID -> "Test-10c7bffb-8c68-4470-8940-f9281c888113"
 ]
Out[5]=

You can define your own method for generating a TestID for each test:

In[6]:=
ResourceFunction["OpenTestWritingPalette"][
  Function[RandomInteger[1000]]];
In[7]:=
Factor[x^2 - y^2]
Out[7]=
In[8]:=
VerificationTest[
 	Factor[x^2 - y^2]
 	,
 	(x - y)*(x + y) ,
 	TestID -> "730"
 ]
Out[8]=

Use None if you don't want to automatically generate test IDs:

In[9]:=
ResourceFunction["OpenTestWritingPalette"][None];
In[10]:=
StringRiffle[{"Free lunch", 4, "every", 1}]
Out[10]=
In[11]:=
VerificationTest[
 	StringRiffle[{"Free lunch",  4, "every", 1}]
 	,
 	"Free lunch 4 every 1" ]
Out[11]=

You can also convert tests in a testing notebook. Open the example notebook, highlight tests and use the Copy tests button:

In[12]:=
NotebookOpen[
 FindFile@FileNameJoin[{"ExampleData", "ExampleTestFile.nb"}]]
Out[12]=

Paste them:

Neat Examples (2) 

Create a TestID generator that automatically increments with each new test:

In[13]:=
testIDFun[str_String] := Module[{i = 1},
   Function[StringRiffle[{str, i++}, "-"]]
   ];
ResourceFunction["OpenTestWritingPalette"][testIDFun["MyTest"]];

Create some tests:

In[14]:=
Select[Range[20], EvenQ]
Out[14]=
In[15]:=
Select[Range[20], PrimeQ]
Out[15]=

Publisher

Sjoerd Smit

Version History

  • 2.0.0 – 17 April 2024
  • 1.1.2 – 15 November 2022
  • 1.1.1 – 28 October 2022
  • 1.1.0 – 25 October 2022
  • 1.0.1 – 13 September 2022
  • 1.0.0 – 26 July 2022

Related Resources

License Information