Function Repository Resource:

OpenTestWritingPalette

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 TestCreate 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 TestCreate expressions from tests in testing notebooks.
Copy tests will 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 directly to the output file. This is essentially a copy-paste shortcut.
The following options can be given:
SameTestInheritedSameTest option for generated tests
TimeConstraintInheritedTimeConstraint option for generated tests
MemoryConstraintInheritedMemoryConstraint option for generated tests
MetaInformationInheritedMetaInformation option for generated tests
"TestHead"TestCreatetype of test generated
"TestFile"Nonefile to append tests to
ResourceFunction["OpenTestWritingPalette"] takes the options SameTest, TimeConstraint, MemoryConstraint and MetaInformation from TestCreate. These option values will be used as the default settings for the generated tests. Option values can be wrapped in Hold to pass them into the tests unevaluated.
The "TestHead" option can be used to specify different testing heads such as VerificationTest or Test from the MUnit package.
The "TestFile" option can be used to specify the output file where results will be appended when using the "Add tests to file" button.
The defaults for TestCreate options can be temporarily overwritten in the palette itself using the Set buttons in the "Option settings" section. In the dialog box that pops up, new values can be typed. These values will remain unevaluated unless wrapped in Evaluate, so it's not necessary to use Hold to keep them in unevaluated form.

Examples

Basic Examples (4) 

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 an input to test and then select the CellGroup (or multiple cell groups) that holds the input and output. Then click Create tests to create one or more TestCreate expressions:

In[2]:=
1 + 1
Out[2]=
In[3]:=
TestCreate[
 	1 + 1
 	,
 	2
 	,
 	TestID -> "Test-eba36c9e-9d6c-49fe-adaa-c2e81c83d3f0"
 ]
Out[3]=

Run the test:

In[4]:=
TestEvaluate[%]
Out[4]=

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[5]:=
1/0
Out[5]=
In[6]:=
TestCreate[
 	1/0
 	,
 	ComplexInfinity
 	,
 	{Power::infy}
 	,
 	TestID -> "Test-fac24c93-bd0a-48fc-89a8-eb375084871d"
 ]
Out[6]=
In[7]:=
TestEvaluate[%]
Out[7]=

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

In[8]:=
ResourceFunction["OpenTestWritingPalette"][
  Function[RandomInteger[1000]]];
In[9]:=
Factor[x^2 - y^2]
Out[9]=
In[10]:=
TestCreate[
 	Factor[x^2 - y^2]
 	,
 	(x - y)*(x + y)
 	,
 	TestID -> "210"
 ]
Out[10]=

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

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

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

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

Paste them:

Options (5) 

SameTest (2) 

Create tests with a different SameTest:

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

Create a test:

In[15]:=
1 + 2
Out[15]=
In[16]:=
TestCreate[
 	1 + 2
 	,
 	3
 	,
 	SameTest -> Equal, TestID -> "Test-7f5c718f-8c5e-4e76-91d0-1ef67b6b61cf"
 ]
Out[16]=
In[17]:=
TestEvaluate[%]
Out[17]=

You can change the SameTest by 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[18]:=
1 + 3
Out[18]=
In[19]:=
TestCreate[
 	1 + 3
 	,
 	4 ,
 	SameTest -> (MatchQ[#1, #2] &), TestID -> "Test-b30f62cb-7bf9-45e6-87d7-3d4843404505"
 ]
Out[19]=
In[20]:=
TestEvaluate[%]
Out[20]=

Clicking the 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[21]:=
myTest = Function[Abs[#1 - #2] < 0.001];
ResourceFunction["OpenTestWritingPalette"][SameTest -> Hold[myTest]]
Out[22]=

Create a test:

In[23]:=
Exp[5.]
Out[23]=
In[24]:=
TestCreate[
 	Exp[5.]
 	,
 	148.4131591025766`
 	,
 	SameTest -> myTest, TestID -> "Test-70b3ea10-b270-4bc8-9b2e-e9beada73fef"
 ]
Out[24]=
In[25]:=
TestEvaluate[%]
Out[25]=

If you use the Set button in the palette to change the SameTest to myTest, you don't need to wrap it in Hold.

TimeConstraint (1) 

Create tests with time constraints:

In[26]:=
ResourceFunction["OpenTestWritingPalette"][TimeConstraint -> 3]
Out[26]=
In[27]:=
Pause[4];
1 + 1
Out[28]=
In[29]:=
TestCreate[
 	Pause[4]; 1 + 1
 	,
 	2 ,
 	TimeConstraint -> 3, TestID -> "Test-d6375b58-0ee8-45ab-aa11-270563edfbf5"
 ]
Out[29]=
In[30]:=
TestEvaluate[%]
Out[30]=

MemoryConstraint (1) 

Create tests with memory constraints:

In[31]:=
ResourceFunction["OpenTestWritingPalette"][MemoryConstraint -> 1000]
Out[31]=
In[32]:=
Length@Range[10^6]
Out[32]=
In[33]:=
TestCreate[
 	Length[Range[10^6]]
 	,
 	1000000 ,
 	MemoryConstraint -> 1000, TestID -> "Test-48bf440c-bb81-4795-91fd-139fd7e85974"
 ]
Out[33]=
In[34]:=
TestEvaluate[%]
Out[34]=

TestHead (1) 

Change the type of test generated by the tool:

In[35]:=
ResourceFunction["OpenTestWritingPalette"][
 "TestHead" -> VerificationTest]
Out[35]=
In[36]:=
1 + 1
Out[36]=

Neat Examples (2) 

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

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

Create some tests:

In[38]:=
Select[Range[20], EvenQ]
Out[38]=
In[39]:=
Select[Range[20], PrimeQ]
Out[39]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 13.0 (December 2021) or above

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