Wolfram Research

Function Repository Resource:

OpenTestWritingPalette (1.1.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 and Options

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.
ResourceFunction["OpenTestWritingPalette"] takes the options SameTest, TimeConstraint and MemoryConstraint from VerificationTest. These option values will be used as the default settings for the generated tests. These defaults can be temporarily overwritten in the palette itself. Option values can be wrapped in Hold to pass them into the tests unevaluated.

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 theCellGroup (or multiple cell groups) that holds the input and output. Then press Create tests to create a VerificationTest (or multiple) 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 with the "Add tests to file" button.

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:

Options (4) 

SameTest (2) 

Create tests with a different SameTest:

In[13]:=
ResourceFunction["OpenTestWritingPalette"][SameTest -> Equal]
Out[13]=

Create a test:

In[14]:=
1 + 2
Out[14]=
In[15]:=
VerificationTest[
 	1 + 2
 	,
 	3 ,
 	SameTest -> Equal, TestID -> "Test-76b089bf-7bd2-4e8e-a410-8f0d07cd1e6e"
 ]
Out[15]=

You can change the SameTestby clicking the Set button on the palette and entering a new one in the dialog:

A Tooltip on the SameTest text will show the test currently being used:

Create a new test:

In[16]:=
1 + 3
Out[16]=

Pressing Reset button on the palette will revert the test back to the original value (which is Equal in this case since that was the option value the palette was created with):


If you have a custom test function assigned to a variable, wrap it in Hold to prevent it from evaluating before being inserted into the test:

In[17]:=
myTest = Function[Abs[#1 - #2] < 0.001];
ResourceFunction["OpenTestWritingPalette"][SameTest -> Hold[myTest]]
Out[18]=
In[19]:=
ResourceFunction["OpenTestWritingPalette"][]
Out[19]=

Create a test:

In[20]:=
Exp[5.]
Out[20]=
In[21]:=
VerificationTest[
 	Exp[5.]
 	,
 	148.4131591025766` ,
 	SameTest -> myTest, TestID -> "Test-b3a968fc-235c-4aec-8403-4a97d6b36692"
 ]
Out[21]=

TimeConstraint (1) 

Create tests with time constraints:

In[22]:=
ResourceFunction["OpenTestWritingPalette"][TimeConstraint -> 3]
Out[22]=
In[23]:=
Pause[4];
1 + 1
Out[24]=
In[25]:=
VerificationTest[
 	Pause[4]; 1 + 1
 	,
 	2 ,
 	TimeConstraint -> 3, TestID -> "Test-d6375b58-0ee8-45ab-aa11-270563edfbf5"
 ]
Out[25]=

MemoryConstraint (1) 

Create tests with memory constraints:

In[26]:=
ResourceFunction["OpenTestWritingPalette"][MemoryConstraint -> 1000]
Out[26]=
In[27]:=
Length@Range[10^6]
Out[27]=
In[28]:=
VerificationTest[
 	Length[Range[10^6]]
 	,
 	1000000 ,
 	MemoryConstraint -> 1000, TestID -> "Test-48bf440c-bb81-4795-91fd-139fd7e85974"
 ]
Out[28]=

Neat Examples (2) 

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

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

Create some tests:

In[30]:=
Select[Range[20], EvenQ]
Out[30]=
In[31]:=
Select[Range[20], PrimeQ]
Out[31]=

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