TestCreate [input]
create a TestObject to determine whether input evaluates to True .
TestCreate [input,expected]
create a TestObject to determine whether input evaluates to expected.
TestCreate [input,expected,messages]
create a TestObject that is expected to generate the list of message names messages.
TestCreate
TestCreate [input]
create a TestObject to determine whether input evaluates to True .
TestCreate [input,expected]
create a TestObject to determine whether input evaluates to expected.
TestCreate [input,expected,messages]
create a TestObject that is expected to generate the list of message names messages.
Details and Options
- TestCreate returns a TestObject .
- TestCreate has attribute HoldAllComplete , therefore it does not immediately evaluate input or expected.
- Use TestReport or TestEvaluate to run a test created by TestCreate .
- TestCreate has the following options:
-
- TestID Automatic creates a unique ID by doing a hash of the test expression.
Examples
open all close allBasic Examples (4)
Create a test that tests whether the input evaluates to True :
test = TestCreate[Sin[E] > Cos[E]]Evaluate the test:
TestEvaluate[test]Test whether the input evaluates to the expected output:
TestEvaluate[TestCreate[Expand[(1 + x) ^ 3], 1 + 3 x + 3 x^2 + x^3]]When a test fails, it returns an outcome of Failure:
TestEvaluate[TestCreate[2 + 2, 3]]When the input evaluates to the expected output but gives messages, the outcome is MessagesFailure:
TestEvaluate[TestCreate[(1/0), ComplexInfinity]]Add the expected message:
TestEvaluate[TestCreate[1 / 0, ComplexInfinity, {Power::infy}]]Scope (2)
Basic Uses (2)
Perform a test and present the results in a Dataset :
test = TestEvaluate[TestCreate[1 / 0, ComplexInfinity]];
Dataset[test[All]]Test if the results of an NIntegrate are numerically equivalent to Integrate by using an absolute tolerance:
NIntegrate[Sin[x], {x, 0, 2}] - Integrate[Sin[x], {x, 0, 2}]TestEvaluate[TestCreate[
Abs[NIntegrate[Sin[x], {x, 0, 2}] - Integrate[Sin[x], {x, 0, 2}]] < 10^-14, True
]]This can also be done by using the SameTest option:
test = TestCreate[
NIntegrate[Sin[x], {x, 0, 2}],
Integrate[Sin[x], {x, 0, 2}],
SameTest -> (Abs[#1 - #2] ≤ 10^-14&)
];TestEvaluate[test]Options (13)
MemoryConstraint (1)
Run a test until a memory limit is exceeded:
ByteCount[RandomImage[]]test = TestCreate[
Head @ RandomImage[],
Image,
MemoryConstraint -> Quantity[80, "Kilobytes"]
];TestEvaluate[test]["ActualOutput"]SameTest (5)
Test whether the input is an integer:
test = TestCreate[
RandomInteger[{1, 10}],
_Integer,
SameTest -> MatchQ
];TestEvaluate[test]Test if the absolute precision of the input is at least 10-12:
test = TestCreate[
1.,
1. + 5 10.^-13,
SameTest -> (Abs[#1 - #2] ≤ 10^-12&)
];TestEvaluate[test]Test if the relative precision is at least 10-5:
test = TestCreate[
1.,
1.0001,
SameTest -> (Abs[(#1 - #2) / #2] ≤ 10^-5&)
];TestEvaluate[test]Test if two expressions are equivalent after simplification:
test = TestCreate[
Cos[x]^2 + Sin[x]^2,
1,
SameTest -> (Simplify[#1] === Simplify[#2]&)
];
TestEvaluate[test]Use Unevaluated to pass unevaluated expressions to the SameTest function:
test = TestCreate[Unevaluated[RandomReal[]], Unevaluated[RandomReal[]]];TestEvaluate[test]You can use a combination of Function and Echo to print the arguments of the SameTest function:
test = TestCreate[
StringJoin[2],
Unevaluated[StringJoin[2]],
{StringJoin::string},
SameTest -> Function[
{left, right},
SameQ @@Echo[HoldComplete[left, right]],
HoldAllComplete
]
];TestEvaluate[test]TestID (4)
Add a TestID to the test:
TestCreate[1 + 1, 2, TestID -> "myTestID"]A TestID can be any expression:
Table[
TestCreate[n + 2 > 0, True, TestID -> {"NumericTest", n}],
{n, 3}
]By default, the TestID is a Hash of the test:
VerificationTest[2 + 2, 4]["TestID"]The TestID will change if the input of the TestCreate expression is different:
VerificationTest[2 + 2, 5]["TestID"]Use CreateUUID to create random identifiers every time:
TestCreate[2 + 2, 5, TestID :> CreateUUID[]]["TestID"]TimeConstraint (1)
Terminate the test if it takes more than one second:
test = TestCreate[Pause[2], Null, TimeConstraint -> Quantity[1, "Seconds"]];TestEvaluate[test]["ActualOutput"]MetaInformation (2)
Store arbitrary metainformation in the TestObject :
test = TestCreate[
1,
2 + 2,
MetaInformation -> <|"$MachineID" -> $MachineID|>
]Access the stored MetaInformation :
test["MetaInformation"]MetaInformation can be any expression:
test = TestCreate[2 + 2, 4, MetaInformation -> {$MachineID, $KernelID}]test["MetaInformation"]Possible Issues (3)
The expected output is evaluated before comparison with the actual output:
test = TestCreate[RandomReal[], RandomReal[]];TestEvaluate[test]Use Unevaluated to compare expressions before evaluation happens:
test = TestCreate[Unevaluated[RandomReal[]], Unevaluated[RandomReal[]]];TestEvaluate[test]Messages generated on evaluation of expected output are not accounted for in the expected messages:
TestEvaluate[TestCreate[ComplexInfinity, 1 / 0]]If SameTest does not evaluate to True or False , a failure type of "SameTestUnevaluated" is reported:
Equal[x, y]TestEvaluate[TestCreate[x, y, SameTest -> Equal]]See Also
Tech Notes
Related Guides
Related Workflows
- Write Unit Tests
History
Text
Wolfram Research (2023), TestCreate, Wolfram Language function, https://reference.wolfram.com/language/ref/TestCreate.html.
CMS
Wolfram Language. 2023. "TestCreate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TestCreate.html.
APA
Wolfram Language. (2023). TestCreate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TestCreate.html
BibTeX
@misc{reference.wolfram_2026_testcreate, author="Wolfram Research", title="{TestCreate}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/TestCreate.html}", note=[Accessed: 14-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_testcreate, organization={Wolfram Research}, title={TestCreate}, year={2023}, url={https://reference.wolfram.com/language/ref/TestCreate.html}, note=[Accessed: 14-August-2026]}