Posted  by 

Run Unit Tests Individually Microsoft Visual Studio Mac

Apr 01, 2017  I am running Visual Studio on Mac (Preview 5, latest) and I am trying to get unit testing with Xunit working. I am running Visual Studio on Mac (Preview 5, latest) and I am trying to get unit testing with Xunit working. Tests not picked up with XUnit testrunner on Visual Studio on Mac.

  1. Run Unit Tests Individually Microsoft Visual Studio Mac
  2. Run Unit Tests Individually Microsoft Visual Studio Mac Osx
  3. Microsoft Visual Studio Mac Download
  4. Run Unit Tests Individually Microsoft Visual Studio Mac C++
-->

This article steps you through creating, running, and customizing a series of unit tests using the Microsoft unit test framework for managed code and Visual Studio Test Explorer. You start with a C# project that is under development, create tests that exercise its code, run the tests, and examine the results. Then you change the project code and rerun the tests.

Create a project to test

Apr 19, 2017  If you are using Visual Studio Enterprise, you can run code coverage on your unit tests. Assuming you have unit tests already set up for your project, this is as simple as going to Test Analyze Code Coverage in the main Visual Studio menu at the top of the IDE. Apr 01, 2013  Unit testing in OS X and Windows. I've installed Xamarin Studio on a mac and pc. I created a solution on the mac, added a unit test project to the solution, added some tests, everything runs and works great. Android, but that doesn't really matter) project that will compile and run Unit Tests from within Xamarin Studio on both OS X.

  1. Open Visual Studio.

  2. On the File menu, select New > Project.

    The New Project dialog box appears.

  3. Under the Visual C# > .NET Core category, choose the Console App (.NET Core) project template.

  4. Name the project Bank, and then click OK.

    The Bank project is created and displayed in Solution Explorer with the Program.cs file open in the code editor.

    Note

    If Program.cs is not open in the editor, double-click the file Program.cs in Solution Explorer to open it.

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

  3. Search for and select the C# Console App (.NET Core) project template, and then click Next.

  4. Name the project Bank, and then click Create.

    The Bank project is created and displayed in Solution Explorer with the Program.cs file open in the code editor.

    Note

    If Program.cs is not open in the editor, double-click the file Program.cs in Solution Explorer to open it.

  1. Replace the contents of Program.cs with the following C# code that defines a class, BankAccount:

  2. Rename the file to BankAccount.cs by right-clicking and choosing Rename in Solution Explorer.

  3. On the Build menu, click Build Solution.

You now have a project with methods you can test. In this article, the tests focus on the Debit method. The Debit method is called when money is withdrawn from an account.

Create a unit test project

  1. On the File menu, select Add > New Project.

    Tip

    You can also right-click on the solution in Solution Explorer and choose Add > New Project.

  1. In the New Project dialog box, expand Installed, expand Visual C#, and then choose Test.

  2. From the list of templates, select MSTest Test Project (.NET Core).

  3. In the Name box, enter BankTests, and then select OK.

    The BankTests project is added to the Bank solution.

  1. Search for and select the C# MSTest Test Project (.NET Core) project template, and then click Next.

  2. Name the project BankTests.

  3. Click Create.

    The BankTests project is added to the Bank solution.

  1. In the BankTests project, add a reference to the Bank project.

    In Solution Explorer, select Dependencies under the BankTests project and then choose Add Reference from the right-click menu.

  2. In the Reference Manager dialog box, expand Projects, select Solution, and then check the Bank item.

  3. Choose OK.

Create the test class

Create a test class to verify the BankAccount class. You can use the UnitTest1.cs file that was generated by the project template, but give the file and class more descriptive names.

Rename a file and class

  1. To rename the file, in Solution Explorer, select the UnitTest1.cs file in the BankTests project. From the right-click menu, choose Rename, and then rename the file to BankAccountTests.cs.
  1. To rename the class, choose Yes in the dialog box that pops up and asks whether you want to also rename references to the code element.
  1. To rename the class, position the cursor on UnitTest1 in the code editor, right-click, and then choose Rename. Type in BankAccountTests and then press Enter.

The BankAccountTests.cs file now contains the following code:

Add a using statement

Add a using statement to the test class to be able to call into the project under test without using fully qualified names. At the top of the class file, add:

Test class requirements

The minimum requirements for a test class are:

Run Unit Tests Individually Microsoft Visual Studio Mac

  • The [TestClass] attribute is required on any class that contains unit test methods that you want to run in Test Explorer.

  • Each test method that you want Test Explorer to recognize must have the [TestMethod] attribute.

You can have other classes in a unit test project that do not have the [TestClass] attribute, and you can have other methods in test classes that do not have the [TestMethod] attribute. You can call these other classes and methods from your test methods.

Create the first test method

In this procedure, you'll write unit test methods to verify the behavior of the Debit method of the BankAccount class.

There are at least three behaviors that need to be checked:

  • The method throws an ArgumentOutOfRangeException if the debit amount is greater than the balance.

  • The method throws an ArgumentOutOfRangeException if the debit amount is less than zero.

  • If the debit amount is valid, the method subtracts the debit amount from the account balance.

Tip

You can delete the default TestMethod1 method, because you won't use it in this walkthrough.

To create a test method

The first test verifies that a valid amount (that is, one that is less than the account balance and greater than zero) withdraws the correct amount from the account. Add the following method to that BankAccountTests class:

The method is straightforward: it sets up a new BankAccount object with a beginning balance and then withdraws a valid amount. It uses the AreEqual method to verify that the ending balance is as expected.

Test method requirements

A test method must meet the following requirements:

  • It's decorated with the [TestMethod] attribute.

  • It returns void.

  • It cannot have parameters.

Build and run the test

  1. On the Build menu, choose Build Solution.

  2. If Test Explorer is not open, open it by choosing Test > Windows > Test Explorer from the top menu bar.

  3. Choose Run All to run the test.

    While the test is running, the status bar at the top of the Test Explorer window is animated. At the end of the test run, the bar turns green if all the test methods pass, or red if any of the tests fail.

    In this case, the test fails.

  4. Select the method in Test Explorer to view the details at the bottom of the window.

Fix your code and rerun your tests

The test result contains a message that describes the failure. For the AreEqual method, the message displays what was expected and what was actually received. You expected the balance to decrease, but instead it increased by the amount of the withdrawal.

The unit test has uncovered a bug: the amount of the withdrawal is added to the account balance when it should be subtracted.

Correct the bug

To correct the error, in the BankAccount.cs file, replace the line:

Discover Sparklines for bringing your data to life with dynamic data representations that are contained within the sheet you are working on. Recommended Charts and Quick Analysis are huge timesavers. Microsoft office mac home student family pack 2011 deutsch.

with:

Rerun the test

In Test Explorer, choose Run All to rerun the test. The red/green bar turns green to indicate that the test passed.

Use unit tests to improve your code

This section describes how an iterative process of analysis, unit test development, and refactoring can help you make your production code more robust and effective.

Analyze the issues

You've created a test method to confirm that a valid amount is correctly deducted in the Debit method. Now, verify that the method throws an ArgumentOutOfRangeException if the debit amount is either:

Run Unit Tests Individually Microsoft Visual Studio Mac Osx

  • greater than the balance, or
  • less than zero.

Create and run new test methods

Create a test method to verify correct behavior when the debit amount is less than zero:

Use the ThrowsException method to assert that the correct exception has been thrown. This method causes the test to fail unless an ArgumentOutOfRangeException is thrown. If you temporarily modify the method under test to throw a more generic ApplicationException when the debit amount is less than zero, the test behaves correctly—that is, it fails.

To test the case when the amount withdrawn is greater than the balance, do the following steps:

  1. Create a new test method named Debit_WhenAmountIsMoreThanBalance_ShouldThrowArgumentOutOfRange.

  2. Copy the method body from Debit_WhenAmountIsLessThanZero_ShouldThrowArgumentOutOfRange to the new method.

  3. Set the debitAmount to a number greater than the balance.

Running the two tests and verify that they pass.

Continue the analysis

The method being tested can be improved further. With the current implementation, we have no way to know which condition (amount > m_balance or amount < 0) led to the exception being thrown during the test. We just know that an ArgumentOutOfRangeException was thrown somewhere in the method. It would be better if we could tell which condition in BankAccount.Debit caused the exception to be thrown (amount > m_balance or amount < 0) so we can be confident that our method is sanity-checking its arguments correctly.

Look at the method being tested (BankAccount.Debit) again, and notice that both conditional statements use an ArgumentOutOfRangeException constructor that just takes name of the argument as a parameter:

There is a constructor you can use that reports far richer information: ArgumentOutOfRangeException(String, Object, String) includes the name of the argument, the argument value, and a user-defined message. You can refactor the method under test to use this constructor. Even better, you can use publicly available type members to specify the errors.

Refactor the code under test

First, define two constants for the error messages at class scope. Put these in the class under test, BankAccount:

Then, modify the two conditional statements in the Debit method:

Microsoft Visual Studio Mac Download

Refactor the test methods

Refactor the test methods by removing the call to Assert.ThrowsException. Wrap the call to Debit() in a try/catch block, catch the specific exception that's expected, and verify its associated message. The StringAssert.Contains method provides the ability to compare two strings.

Now, the Debit_WhenAmountIsMoreThanBalance_ShouldThrowArgumentOutOfRange might look like this:

Retest, rewrite, and reanalyze

Assume there's a bug in the method under test and the Debit method doesn't even throw an ArgumentOutOfRangeException never mind output the correct message with the exception. Currently, the test method doesn't handle this case. If the debitAmount value is valid (that is, less than the balance and greater than zero), no exception is caught, so the assert never fires. Yet, the test method passes. This is not good, because you want the test method to fail if no exception is thrown.

This is a bug in the test method. To resolve the issue, add an Fail assert at the end of the test method to handle the case where no exception is thrown.

Rerunning the test shows that the test now fails if the correct exception is caught. The catch block catches the exception, but the method continues to execute and it fails at the new Fail assert. To resolve this problem, add a return statement after the StringAssert in the catch block. Rerunning the test confirms that you've fixed this problem. The final version of the Debit_WhenAmountIsMoreThanBalance_ShouldThrowArgumentOutOfRange looks like this:

Conclusion

The improvements to the test code led to more robust and informative test methods. But more importantly, they also improved the code under test.

Tip

This walkthrough uses the Microsoft unit test framework for managed code. Test Explorer can also run tests from third-party unit test frameworks that have adapters for Test Explorer. For more information, see Install third-party unit test frameworks.

See also

Run Unit Tests Individually Microsoft Visual Studio Mac C++

For information about how to run tests from a command line, see VSTest.Console.exe command-line options.