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
- 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.
- Update Homebrew
$ brew update
- Install .NET using Homebrew
$ brew install --cask dotnet-sdk
- If you already had installed it before but want to get the latest,
$ brew upgrade dotnet-sdk
- 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.
-
Create a .NET project (aka "Solution")
$ dotnet new sln -o project-with-tests
-
cd project-with-tests
-
Creates a new class library project in the SuperMassive folder.
dotnet new classlib -o SuperMassive
-
Rename Class1.cs to SuperMassive.cs.
-
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.
-
Build
$ dotnet build
-
Add class library to the solution
$ dotnet sln add ./SuperMassive/SuperMassive.csproj
-
Create tests
$ dotnet new xunit -o SuperMassive.Tests
-
Add the tests to the solution
$ dotnet sln add ./SuperMassive.Tests/SuperMassive.Tests.csproj
-
Add SuperMassive as a dependency to SuperMassive.Tests
$ dotnet add ./SuperMassive.Tests/SuperMassive.Tests.csproj reference ./SuperMassive/SuperMassive.csproj
-
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(); } }
-
-
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
-
Go into the source project directory.
$ cd SuperMassive
-
Add a new class.
$ dotnet new class -n Adder
-
Update the class
namespace SuperMassive; public class Adder { public int add(int i, int j) { return i + j; } }
-
Go into the test project directory.
-
Add a new test.
$ dotnet new class -n AdderTests
-
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.
- Make sure to add
-
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)