concept test in category f#

appears as: tests, test, tests, test, The tests, The test
Get Programming with F#: A guide for .NET developers

This is an excerpt from Manning's book Get Programming with F#: A guide for .NET developers.

In this lesson, you’ll look at an alternative way to develop applications than what you’re probably used to in terms of both tools and process. You’ll first learn the pros and cons of techniques such as debugging and unit tests, and then see how to shorten your development cycle through alternatives that F# provides.

Another example occurs when your unit tests appear to be fragile because they test only arbitrary cases. Imagine that you have a method, half(), being tested that takes in a number and returns half of it. To test this, you write a single test case that proves that when calling half() with 10, you get back 5. Why did you pick 10 as the test data? Is it easy to understand what the test is really proving? Have you tested all valid cases?

Finally, a system under test may be too complex for you to cover (or even identify) all the possible permutations with specific test cases. This is particularly common—but not unique—to integration tests. Normally, we unit test lower-level components well, but tend not to have great integration tests; they’re too expensive to write to cover all combinations. But this omission often misses bugs that result from unintended interactions between two components when you finally bring them together. A good real-world example of this is the Mars Climate Orbiter that failed in 1999 because one team wrote a module using English units of measurement, while unknown to them, another team used the metric system! Both components worked well in isolation, but when plugged together, the system failed because of the unit differences. End result: a costly failed spacecraft. (Let’s leave aside the fact that F# has a feature known as Units of Measure that would’ve prevented this.)

3.  Create a standard F# module and enter the following code. It creates a simple function that manually adds the numbers in a list, followed by a test that compares the values with a known good function, List.sum, that you use as a basis.

Listing 43.3. Creating a Web API controller under test
open Capstone8.Controllers open System.Web.Http open System.Net.Http let createController() = let api = Helpers.createInMemApi() 1 let request = new HttpRequestMessage() 1 let config = new HttpConfiguration() 1 new BankAccountController(api, Request = request, Configuration = config)
F# Deep Dives

This is an excerpt from Manning's book F# Deep Dives.

One way to measure the quality of fit between the model and the experience, as it’s called, is to calculate a cumulative binomial distribution. Because this is a book about F# and not actuarial mathematics, we won’t delve any further into the significance of the test. As a developer, you need to ensure that the calculation is performed correctly.

module Scrabble.Tests

open NUnit.Framework
open Scrabble
let [<Test>] ``Value of QUARTZ is 24`` () =
    Assert.AreEqual(24, wordPoints "QUARTZ")
Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

In the previous listing we implemented the convertDataRow function, which takes a string containing a line from the CSV file and returns a tuple containing a label and a numeric value. As a next step we’ll implement a function that takes a list of strings and converts each string to a tuple using convertDataRow. Listing 4.3 shows the function—and a test immediately afterward, parsing a sample list of strings.

void CondPrint<T>(T value, Func<T, bool> test, Func<T, string> format) {
if (test(value)) Console.WriteLine(format(value));
}
Listing 8.3. Executing tests (C#)

When we’ve discussed testing so far, we’ve usually talked about checking whether the code works immediately after writing it in the F# Interactive shell. If you’re a veteran of unit testing, you may well have been thinking to yourself that a test which can’t be reproduced later on is hardly worth running. Well, let’s see how this kind of test can evolve into a unit test.

We can see that we genuinely have two instances: a test for reference equality ❶ returns false. If we use an overridden Equals method or the standard F# operator for testing equality ❷, the runtime will use structural equality, and it will report that the values are equal. First the two tuple values are compared for structural equality, then the two strings are compared.