-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFsCheckTutorial.html
More file actions
121 lines (116 loc) · 7.38 KB
/
FsCheckTutorial.html
File metadata and controls
121 lines (116 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--
The Testing code with FsCheck parameters will be replaced with the
document title extracted from the <h1> element or
file name, if there is no <h1> heading
-->
<title>Testing code with FsCheck</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<script src="styles/tips.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function init()
{
try {
websocket = new WebSocket("ws://" + window.location.hostname + ":" + window.location.port + "/websocket");
websocket.onmessage = function(evt) { location.reload(); };
} catch (e) { /* silently ignore lack of websockets */ }
}
window.addEventListener("load", init, false);
</script>
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="masthead">
<ul class="nav nav-pills pull-right">
<li><a href="index.html">Index</a></li>
<!--<li><a href="http://fsharp.github.io/FSharp.Data/">FSharp.Data</a></li>
<li><a href="http://fslab.org/Deedle">Deedle</a></li>
<li><a href="http://fslab.org/RProvider">R Provider</a></li>
<li><a href="https://fslab.org/XPlot/">XPlot</a></li>
<li><a href="https://fslab.org/XPlot/">FSharp.Charting</a></li>
<li><a href="https://numerics.mathdotnet.com/">Math.NET Numerics</a></li>-->
</ul>
<h3 class="muted">Practical Guide to Commodity Strategist</h3>
</div>
<hr />
<div class="row" style="margin-top:30px">
<div class="span1"></div>
<div class="span10" id="main">
<h1>Testing code with FsCheck</h1>
<p>Testing code is an important part of writing code. Randomized domain testing is a
good way to test the code throughly and identify issues from a different perspective.
For this, <a href="https://fscheck.github.io/FsCheck/">FsCheck</a> is a very good tool. Read this good introduction for <a href="https://fsharpforfunandprofit.com/posts/property-based-testing/">Property based testing </a>.</p>
<h2>A quick example</h2>
<p>If we want to a test that works for all small int bewteen 1 and 100, we could define a random test domain
that test only the necessary range, as shown in the following code.
The test would often fail as we are testing for a smaller range.</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
<span class="l">11: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="prep">#I</span> <span class="s">".paket/load/"</span>
<span class="prep">#load</span> <span class="s">"FsCheck.fsx"</span>
<span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 1)" onmouseover="showTip(event, 'fs1', 1)" class="i">FsCheck</span>
<span class="c">///int arb filtered with range </span>
<span class="k">type</span> <span onmouseout="hideTip(event, 'fs2', 2)" onmouseover="showTip(event, 'fs2', 2)" class="t">SmallInt</span> <span class="o">=</span>
<span class="k">static</span> <span class="k">member</span> <span onmouseout="hideTip(event, 'fs3', 3)" onmouseover="showTip(event, 'fs3', 3)" class="f">Int</span>() <span class="o">=</span>
<span class="i">Arb</span><span class="o">.</span><span class="i">Default</span><span class="o">.</span><span class="i">Int32</span>()
<span class="o">|></span> <span class="i">Arb</span><span class="o">.</span><span class="i">filter</span> (<span class="k">fun</span> <span class="i">t</span> <span class="k">-></span> (<span class="i">t</span> <span class="o">></span> <span class="n">1</span>) <span class="o">&&</span> (<span class="i">t</span> <span class="o"><=</span> <span class="n">100</span>))
<span class="k">let</span> <span class="i">testsmallint</span> <span onmouseout="hideTip(event, 'fs4', 4)" onmouseover="showTip(event, 'fs4', 4)" class="i">s</span> <span class="o">=</span>
(<span onmouseout="hideTip(event, 'fs4', 5)" onmouseover="showTip(event, 'fs4', 5)" class="i">s</span> <span class="o">></span> <span class="n">1</span> <span class="o">&&</span> <span onmouseout="hideTip(event, 'fs4', 6)" onmouseover="showTip(event, 'fs4', 6)" class="i">s</span> <span class="o"><</span> <span class="n">11</span>) <span class="o">|@</span> <span class="s">"Input is between 1 and 11"</span>
</code></pre></td>
</tr>
</table>
<p>The test can be ran using Check.One with arguments to
shows the passed tests inputs as well as failed cases with shrink</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="i">Check</span><span class="o">.</span><span class="i">One</span>({ <span class="i">Config</span><span class="o">.</span><span class="i">Verbose</span> <span class="k">with</span> <span class="i">MaxTest</span> <span class="o">=</span> <span class="n">5</span>; <span class="i">Arbitrary</span> <span class="o">=</span> [ <span onmouseout="hideTip(event, 'fs5', 7)" onmouseover="showTip(event, 'fs5', 7)" class="i">typeof</span><span class="o"><</span><span onmouseout="hideTip(event, 'fs2', 8)" onmouseover="showTip(event, 'fs2', 8)" class="i">SmallInt</span><span class="o">></span> ] }, <span class="i">testsmallint</span>)
</code></pre></td>
</tr>
</table>
<table class="pre"><tr><td><pre><code>0:
12
shrink:
11
Falsifiable, after 1 test (1 shrink) (StdGen (1784431804,296632552)):
Label of failing property: Input is between 1 and 11
Original:
12
Shrunk:
11</code></pre></td></tr></table>
<div class="tip" id="fs1">namespace FsCheck</div>
<div class="tip" id="fs2">type SmallInt =<br />  static member Int : unit -> 'a<br /><br />Full name: FsCheckTutorial.SmallInt<br /><em><br /><br />int arb filtered with range </em></div>
<div class="tip" id="fs3">static member SmallInt.Int : unit -> 'a<br /><br />Full name: FsCheckTutorial.SmallInt.Int</div>
<div class="tip" id="fs4">val s : int</div>
<div class="tip" id="fs5">val typeof<'T> : System.Type<br /><br />Full name: Microsoft.FSharp.Core.Operators.typeof</div>
</div>
<div class="span1"></div>
</div>
<hr style="margin-top:50px;"/>
<footer class="footer" style="text-align:center">
Generated from <a href="https://github.com/xqguo/PracticalStrategist"> source code here</a>.<br />
Submit <a href="https://github.com/xqguo/PracticalStrategist/issues">feedback or issues on GitHub</a>
</footer>
</div>
</body>
</html>