Getting Started with .NET Development on Linux with .NET Core 2.1 — Part 1

From within the directory containing the .sln file execute the following commands:# create the console application project~$ dotnet new console -o hwstats.app# add the new console application project to the sln~$ dotnet sln add hwstats.appThe output from executing the above should closely resemble the image below:Finally create the unit test project and add the application project, hwstats.app, as a reference to this project so that the unit tests will be able to access the application code to execute the tests..Add the unit test project to the solution after creating it:# create the unit test project~$ dotnet new mstest -o hwstats.tests# add the unit test project to the sln~$ dotnet sln add hwstats.tests# add the main application as a project reference to the test project~$ dotnet add hwstats.tests/hwstats.tests.csproj reference hwstats.app/hwstats.app.csprojThe output should resemble the image below if the above executed successfully:The directory structure for the solution and projects should appear similar to the image below:Directory Structure of SolutionEnsure the solution builds without errors by executing dotnet build hwstats.sln..The output from the build should indicate success as in the image belowBuild the SolutionFinally ensure the main application executes without error by executing dotnet run -p hwstats.app..The default behavior, if sucessful, should be for the application to print ‘Hello World!’ to stdout.Design and ImplementationWith the requirements defined and the basic project structured created the next step in the software development life cycle is the design phase..Because this is a simple application to demonstrate cross platform development with .NET Core the SDLC will be condensed into a “design and implement by feel” process.Because a data type that can represent a height/weight pair seems useful let’s create a new class in the hwstats.app project via touch hwstats.app/Observation.cs and a corresponding unit test file via touch hwstats.tests/ObservationTests.cs..Assigning a value less than or equal to zero to height or weight should throw an ArgumentException and of course the obvious case of assigning a positive value should be retained for that variable, i.e..when assigning 166.0f to Height the Height variable should retain that value..The initial naive implementation of a height/weight observation is below, the unit test (further below) can be written against this with the expected failures, i.e..setting a negative value for height or weight should throw an exception of type ArgumentException.hwstats.app/Observation.cshwstats.tests/ObservationTests.csRunning the unit tests with dotnet test hwstats.tests, from within the top level solution folder, indicates two failed test cases because TestSetNegativeHeight and TestSetNegativeWeight expect exceptions that are not begin thrown in the implementations of Observation.Height and Observation.Weight.Failing Test CasesAfter correcting Observations.cs to throw an exception, of type ArgumentExcpetion, when Height or Weight are set to values less than or equal to zero all the test cases pass.Corrected hwstats.app/Observation.cs Throws Exceptions Where Required to Pass Unit TestExecute the unit tests again with dotnet test hwstats.tests, from within the top level solution folder, and everything should pass as expected.Unit Tests Pass with Correct ImplementationNext TimePart 2 will continue with the design and implementation of this simple project to demonstrate .NET development on Linux.. More details

Leave a Reply