Jason Kim's Blog

Setup a .NET project on Mac OSX
2024-08-31

We will create a new .NET project on Mac OSX using VSCode.

If you want to skip ahead and just look at the final code setup, check out this github repo.

Setup .NET Development environment

  1. Follow instructions on this link.

Updating to the latest .NET environment

There's a chance that you had prepped .NET development environment years ago. In such case, we want to update to the latest .NET development environment.

  1. Update Homebrew
    • $ brew update
  2. Install .NET using Homebrew
    • $ brew install --cask dotnet-sdk
  3. If you already had installed it before but want to get the latest,
    • $ brew upgrade dotnet-sdk
  4. Verify
    • $ dotnet --version
    • Should 8.0.401 as of Aug 31, 2024

Setup a .NET project

  • I am following the instruction here.
  • If you want to follow the same instructions with my concise commentary, follow instructions below.
  1. Create a .NET project (aka "Solution")

    • $ dotnet new sln -o project-with-tests
  2. cd project-with-tests

  3. Creates a new class library project in the SuperMassive folder.

    • dotnet new classlib -o SuperMassive
  4. Rename Class1.cs to SuperMassive.cs.

  5. Inside the project, have the following content.

    namespace SuperMassiveNS;
    
    public class SuperMassive
    {
    
    }
    
  • Notice that the namespace is SuperMassiveNS in order to avoid collision with the class name.
  1. Build

    • $ dotnet build
  2. Add class library to the solution

    • $ dotnet sln add ./SuperMassive/SuperMassive.csproj
  3. Create tests

    • $ dotnet new xunit -o SuperMassive.Tests
  4. Add the tests to the solution

    • $ dotnet sln add ./SuperMassive.Tests/SuperMassive.Tests.csproj
  5. Add SuperMassive as a dependency to SuperMassive.Tests

    • $ dotnet add ./SuperMassive.Tests/SuperMassive.Tests.csproj reference ./SuperMassive/SuperMassive.csproj
  6. Create a test to check that the tests can reference the source class.

    • Rename the test file name

      • SuperMassiveTests.cs
    • Update the content of the test

      namespace SuperMassive.Tests;
      
      public class SuperMassiveTests
      {
          [Fact]
          public void ReferenceSourceTest()
          {
              var superMassive = new SuperMassiveNS.SuperMassive();
          }
      }
      
  7. Run the tests!

    • $ dotnet test
    • If you want to run tests with logging
      • $ dotnet test --logger "console;verbosity=detailed"

Adding more source files and tests

  1. Go into the source project directory.

    • $ cd SuperMassive
  2. Add a new class.

    • $ dotnet new class -n Adder
  3. Update the class

    namespace SuperMassive;
    
    public class Adder
    {
      public int add(int i, int j) {
        return i + j;
      }
    }
    
  4. Go into the test project directory.

  5. Add a new test.

    • $ dotnet new class -n AdderTests
  6. Update the test class

    namespace SuperMassive.Tests;
    
    public class AdderTests
    {
      [Fact]
      public void AddTest()
      {
        var adder = new SuperMassive.Adder();
        int result = adder.add(1, 1);
        Assert.Equal(2, result);
      }
    }
    
    • Make sure to add [Fact]
      • Attribute that is applied to a method to indicate that it is a fact that should be run by the test runner.
  7. Run tests

    • $ dotnet test
    • Now you should see 2 tests running
      • Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2, Duration: 1 ms - SuperMassive.Tests.dll (net8.0)