Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Test C# Functions interactively with F# Interactive

I have a set of static utility methods including unit tests. But I'd like to have a more interactive way to employ a testing -> fixing -> compiling cycle (REPL) like in Lisp or Smalltalk where one can immediately execute code in interactive mode. I tried to use F# Interactive to test these methods directly from within the opened C# project in VS 2010, but I didn't get it to work.

I know that I have to load the assembly (#r directive), open the namespace and then can call the methods (and inspect the result). But how do I do it within “F# Interactive” in Visual Studio 2010? I know it is possible with the “Immediate” window available in debug mode, but I want to do it within F# Interactive in "design mode", when I'm writing the code.

like image 760
Nico Avatar asked Nov 25 '10 16:11

Nico


People also ask

Can you unit test C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

What is test function C?

The strcmp() is a built-in function available in "string. h" header file. It is used for comparing the two strings. It returns 0 if both are same strings. It returns positive value greater than 0 if first string is greater than second string, otherwise it returns negative value.


1 Answers

You need to include the path to your project using the #I directive then you may load your assembly and use it. I wrote a simple C# console app try this and got this to work.

using System;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            PrintMessage();
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
            Console.WriteLine();
        }

        public static void PrintMessage()
        {
            Console.WriteLine("MESSAGE!");
        }
    }
}

Then in F# interactive:

> #I "full path to debug directory";;

--> Added 'full path to debug directory' to library include path

> #r "ConsoleApplication1.exe";;

--> Referenced 'full path to debug directory\ConsoleApplication1.exe'

> open ConsoleApplication1;;
> Program.PrintMessage();;
MESSAGE!
val it : unit = ()

So it definitely works, you just need to compile your projects first. Just remember to reset your session to release your assembly beforehand.

like image 167
Jeff Mercado Avatar answered Sep 18 '22 07:09

Jeff Mercado