({{, (RefLink[Assert, paclet:ref/Assert])[test] 
represents the assertion that test is (RefLink[True, paclet:ref/True]). If assertions have been enabled, test is evaluated when the assertion is encountered. If test is not (RefLink[True, paclet:ref/True]), then an assertion failure is generated.}, {, (RefLink[Assert, paclet:ref/Assert])[test,tag]
specifies a tag that will be used to identify the assertion if it fails.}})

OpenersNotesSection(Spacer1)Details

Assertions are often placed in code to describe and check assumptions made by the programmer.

By default, assertions are disabled in an ordinary Wolfram Language session, but are enabled in the Wolfram System debugger.

(RefLink[On, paclet:ref/On])[Assert] enables assertions in a Wolfram Language session, and causes a message to be generated whenever an assertion fails.

When (RefLink[Assert, paclet:ref/Assert]) appears in a file, the name of the file and the line number at which it appears are by default automatically used as tags for the assertion.

In the Wolfram Language debugger, the failure of an assertion by default generates a breakpoint.

(RefLink[$AssertFunction, paclet:ref/$AssertFunction]) gives a function to be applied to (RefLink[Assert, paclet:ref/Assert])[test,…] when test does not evaluate to (RefLink[True, paclet:ref/True]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (3)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Define a function that contains an assertion depending on a temporary result as the computation runs:

myFun[x_]:=
Block[{y},
y=x^2;
Assert[y>5];
Sin[y]
]

This does not test the assertion since assertions are not enabled:

myFun[1.0]

When assertions are enabled with (RefLink[On, paclet:ref/On]), a message is produced:

On[Assert];
myFun[1.0]

Off[Assert];

If you load a package that contains functions with assertions, the name of the package and line number is remembered:

FilePrint["ExampleData`FunctionWithAssert`"]

<<ExampleData`FunctionWithAssert`

This shows how the line information and package name are stored in the (RefLink[Assert, paclet:ref/Assert]) expression:

??Compute

Now, if the assertion is triggered, the message tells you the name of the package and the line number where the assertion is located:

On[Assert];
Compute[1.0]

Disable assertions:

Off[Assert];

You can change the behavior when an assertion fails by assigning to (RefLink[$AssertFunction, paclet:ref/$AssertFunction]). This function throws an exception when an assertion fails:

$AssertFunction = Throw[{##}]& ;

<<ExampleData`FunctionWithAssert`

Here the exception thrown by the assertion function is caught:

Catch[ Compute[ 1.0]]

Off[Assert]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Print, paclet:ref/Print])  ▪  (RefLink[Message, paclet:ref/Message])  ▪  (RefLink[Check, paclet:ref/Check])  ▪  (RefLink[On, paclet:ref/On])  ▪  (RefLink[Abort, paclet:ref/Abort])  ▪  (RefLink[$AssertFunction, paclet:ref/$AssertFunction])  ▪  (RefLink[Failure, paclet:ref/Failure])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Tuning & Debugging, paclet:guide/TuningAndDebugging])}, {▪(RefLinkPlain[Symbolic Execution History, paclet:guide/SymbolicExecutionHistory])}, {▪(RefLinkPlain[Messages, paclet:guide/Messages])}, {▪(RefLinkPlain[Package Development, paclet:guide/PackageDevelopment])}})}})