Non-ide development

Hi,

I’ve been learning F# for a while and really love it.
Now I’m ready to move past a simple one-thing project, and thought I’d do the “Ray Tracer Challenge” book but I’m getting lost on how to proceed with integrating testing into a project.
My very strong preference is to use vi and command line tools - because that’s how I’ve been developing for 40yrs - but I can’t find much that explains how dotnet/F# projects fit together; every book I’ve looked at assumes you’ll be using and IDE, Visual Studio or similar.

If nothing else, I need to know how to add testing into a console app - that would get me started on the book.

So, can anyone point me to some good resources? Are there any good resources - or do I need to give up and find an IDE to hold my hand?

thanks
Jonathan.

Hi,

To work with the command line tools use the dotnet CLI (Command Line Interface). You can read about it here dotnet CLI documentation

For CLI comands specific to F# read the F# CLI documentation

To create an F# Console application you can use the following command

dotnet new console -lang "F#" -o ConsoleApp

This command will create a directory/folder named ConsoleApp

Then you can go to the ConsoleApp directory/folder and run the app using the following command

dotnet run

you should see a text “Hello from F#”.

To add a testing project you can use the following command

dotnet new xunit -o Tests

This command create a new directory/folder named Tests and create a project using the xUnit framework to do tests. You can use MSTests or nUnit framework too.

To run the tests you can use the following command inside the foler Tests

dotnet test

About the IDE

If you don’t like to code with an IDE I would recommend you VS Code with the Ionide extension (to work with F#). It’s a ligthweight standalone source code editor that runs on Windows, macOS, and Linux with debugging capabilities. You should give it a try.

You should definitely be able to make good progress without an IDE. Even when I use an IDE, I still really only use it for stuff like Go To Definition, and autocomplete and type definitions when I hover on stuff. I use the command line dotnet tool for managing projects & solutions, running tests, etc.

Now, the most oft recommended way to add tests is to have a separate project for the tests, which is pretty easy to setup. You can do dotnet new --list -lang f# to see all of the F#-compatible project templates available on your machine. (Personally I would recommend NUnit or XUnit and not MSTest, since MSTest seems to me especially tailored for C#). Once you have multiple projects, you probably want a “solution” to contain them. So I can do

> dotnet new console -lang f# -n ProdProj
> dotnet new nunit -lang f# -n TestProj
> dotnet new sln -n MySolution
> dotnet sln add ProdProj
> dotnet sln add TestProj
> cd TestProj
> dotnet add reference ../ProdProj

(You probably have that first step already done for your console app). Now you can run dotnet build (or dotnet build MySolution.sln) to build both projects, or dotnet test to run all the tests in the test project.

Sometimes I do like to have my tests in the same project, even if that’s not the recommended approach. What I would do in that case is take a look at the .fsproj that gets created when you run dotnet new nunit -lang f#, and figure out what things to port over to your prod project. In this case, I think the important bits are

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
    <PackageReference Include="NUnit" Version="3.13.2" />
    <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
    <PackageReference Include="coverlet.collector" Version="3.1.0" />
  </ItemGroup>

Copy that into your console app, and now you can add tests directly into that project.

As far as resources go, I mostly just use the --help docs with all the dotnet commands, and then just run google searches on any nodes in the fsproj that I don’t know what they do. There might be some nice comprehensive docs on that stuff, but I don’t know them. If you’re searching for fsproj details, you might want to sometimes try searching with “csproj” instead as your search term, since there’s probably 100x more resources for csproj stuff online, and the two have very similar structures.

A good reference for the project file properties and items can be found on the SDK-specific pages here. You can pick from the different main SDK types (Microsoft.NET.Sdk, Microsoft.NET.Sdk.Web, etc) on the left side to get more details.

image

1 Like

Wow … thanks ntwilson for that - brilliant.
I’ve got a project setup and ran a test and everything!

I can dig into F# deeper now.

Thanks

1 Like

If you are interested, I have implemented a decent amount of The Ray Tracer Challenge book in F#. There’s a substantial amount of organization, documentation, and most importantly, tests. I use FsUnit.Xunit for my testing. I like the simple API provided by FsUnit, and I like the simpler aspect of xUnit over NUnit. (I just like declaring my tests as facts for whatever reason.)

If you’re in the RayTracerChallenge directory, the one with the RayTracerChallenge.sln solution file, then you can simply run dotnet test to run the tests. Like the others have said, the dotnet CLI is your friend for both non-IDE and IDE development.