Creating a project in VS Code

Now that your VS Code IDE is properly installed with the C# extension enabled, you are ready to create your first project.

With VS Code open, choose Open Folder from the File menu. Choose a location that is easily accessible. Many developers will create a Development folder on the root of their drive. Whatever convention you're used to will be fine. You now need to create an MSTest project.

Create a new folder named Sample. Open the Integrated Terminal window from the View menu or by using the shortcut keys (Ctrl + `). From within the Terminal window, type dotnet new mstest and hit Enter. Now, you need to restore your packages by typing dotnet restore into the Terminal window and hitting Enter.

You should now see a file named UnitTest1.cs within the Sample folder. If you open the file, it should look something like this:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Sample
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}

Change the first test method to an ItExists test. Do this by changing the name to ItExists and trying to declare an instance to a class that does not yet exist:

var sampleClass = new SampleClass();

You should see that your sample application will not compile and you will have received the error message, The type or namespace 'SampleClass' could not be found (are you missing a using directive or an assembly reference?).

Now that you have a test failure (remember, failing to compile counts as a failing test in this instance), it's safe to move on to the Green step in our red, green, refactor cycle. Make the test pass by creating a definition for SampleClass. Feel free to create the class in the same file as your unit tests, just to get you started. This can always be extracted and moved to a more appropriate location later:

public class SampleClass
{
}

Now that you've made the change, run the test command dotnet test and see the results:

Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.

Continue exploring VS Code and growing your new class through tests. The C# and .NET examples throughout the rest of the book will be using Visual Studio Community. If you prefer, you may choose to stick with VS Code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.252.238