Do you follow the standard naming conventions for tests?

Updated by Tiago Araújo [SSW] 1 year ago. See history

123

Ensuring a consistent and organized approach to testing is pivotal in any development process. Do you adhere to the established standard naming conventions for tests? Let's explore the importance of this practice and its impact on the efficiency and clarity of your testing procedures.

> As well as keeping your code tidy, using this naming convention also allows you to use TestDriven.Net's 'Go To Test/Code' command.
> This navigates between your tests and code under test (and back). This is something that test-driven developers end up doing a lot.
> Screen captures at <https://weblogs.asp.net/nunitaddin/testdriven-net-3-0-all-systems-go>
>
> - Jamie Cansdale
| **Test Object**                                           | **Recommended Style**         | **Example**                                                                                                 |
| --------------------------------------------------------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------- |
| Project Name                                              | Tests.[Testtypes].Projectname | Tests.Unit.Common,Tests.Unit.WebFrontend,Test.Integration.MainWCFService                                    |
| Tests.Functional.SilverlightUI, Tests.Functional.WebUI \* |
| Test Fixture Name                                         | [Type]Tests                   | OrdersTests, CustomerTests, DeveloperTests                                                                  |
| Test Case                                                 | [Function]Test                | NullableIntTryParse_NumberIsValid1_Return1, StringHelperEncodeTo64_EncodeAndUnencodeString_ReturnSameString |
| Set Up                                                    | SetUp                         |                                                                                                             |
| Tear Down                                                 | TearDown                      |                                                                                                             |

\* Test types are categorized into "Unit" "Integration" or "Functional" tests, as explained in "[the different types of test you can have](/different-types-of-testing)"

The main reason why we are categorizing tests is so that we can run different test suites. Eg.

- Unit tests on Gated Checkin
- Integration tests after each check in on the build server
- All tests including the functional tests in the nightly build

#### Samples for Naming of test projects

**Test.Unit.WebUI:** This test project, tests the WebUI project, and is independent of external resources.
That means all tests must pass.

**Test.Integration.WebUI:** This test project tests the WebUI and depends on other external resources (Eg. probably needs a database, web services, etc.).
That means if any external resource is unavailable, the tests will fail.

**Tests.Functional.SilverlightUI:** Tests the Silverlight UI from an end-user perspective by clicking around in the application

<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "goodExample",
    figure: 'Good example - Naming for a Unit Test Project',
    shouldDisplay: true
  }}
  src="/uploads/rules/the-standard-naming-conventions-for-tests/UnitTestsProject.jpg"
/>

#### Samples Naming of test methods

```cs
[TestMethod]
 public void Test_Client()
```

<figureEmbed figureEmbed={{
  preset: "badExample",
  figure: 'Bad example: There is no way to guess what this test does; you have to read the source',
  shouldDisplay: true
} } />


```cs
[TestMethod]
 public void PubSubServiceConnectTest_AuctionOk_AuctionInfoReturned()
```

<figureEmbed figureEmbed={{
  preset: "goodExample",
  figure: 'Good example: We are testing PubSubService.Connect under the scenario that the "Auction status is OK" with an expected behaviour that data is returned',
  shouldDisplay: true
} } />


#### Sample Code for Integration Tests

```cs
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using NUnit.Framework;
using SSW.NetToolKit.BusinessService;
using SSW.NetToolKit.DataAccess;
namespace SSW.NETToolkit.IntegrationTests
  {
  [TestFixture]
  Public class CustomerTests
    {
    BusinessRules business=new BusinessRules();

    [Test]
    public void OrderTotal_SimpleExampleInput()
        {
        decimal calculatedGrandTotal = business.CalculateOrderGrandTotal(10248);
        int expected = 440;
        Assert.AreEqual(expected, calculatedGrandTotal, "Calculated grand total didn't match the expect
        }
    [Test]
    public void OderTotal_Discounts()
        {
        decimal calculatedGrandTotal = business.CalculateOrderGrandTotal(10260);
        decimal expected = 1504.65m;
        Assert.AreEqual(expected, calculatedGrandTotal, "Calculated grand total didn't match the expecte
        }
    [Test]
    public void RoundingTest_RoundUp()
        {
        Assert.AreEqual(149.03, business.ApplyRounding(149.0282m), "Incorrect rounding rules applied for
        }
    [Test]
    public void RoundingTest_RoundDown()
        {
        Assert.AreEqual(149.02, business.ApplyRounding(149.0232m), "Incorrect rounding rules applied

        }
    [Test]
    public void RoundingTest_NoRoundingNeeded()
        {
        Assert.AreEqual(149.02, business.ApplyRounding(149.02m), "Incorrect rounding rules applied for

        }
    [Test]
    public void RoundingTest_BorderCondition()
        {
        Assert.AreEqual(149.02, business.ApplyRounding(149.025m), "Incorrect rounding rules applied for
        }
    }
  }
```


<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "default",
    figure: 'This rule is consistent with the Visual Studio default',
    shouldDisplay: true
  }}
  src="/uploads/rules/the-standard-naming-conventions-for-tests/TestGenerationSettings.gif"
/>

**Tip:** You can create a test project using the Unit Test Wizard: Test &gt; Add New Test


<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "default",
    figure: 'Unit Test Wizard 1',
    shouldDisplay: true
  }}
  src="/uploads/rules/the-standard-naming-conventions-for-tests/AddNewTest.gif"
/>


<imageEmbed
  alt="Image"
  size="large"
  showBorder={false}
  figureEmbed={{
    preset: "default",
    figure: 'Unit Test Wizard 2',
    shouldDisplay: true
  }}
  src="/uploads/rules/the-standard-naming-conventions-for-tests/CreateUnitTests.gif"
/>
acknowledgements
related rules