locked
Standard unit test project RRS feed

  • Question

  • User-419562455 posted

    Hi Experts,

    I just want to know your experts opinion because I am making R&D on following points.

    Following are simple one case of my application.

    1) User login --> Click on "Add New User" --> Add details in "Add New User" --> Save User

    Now I have wrote 2 unit test cases for login and save user.

    But my  senior tell me to create unit test case which will satisfy the business scenario. So above whole process should be cover in one test case called "scenario1".

    So is it correct way to write unit test?

    Thanks in Advance

    Jack

    Wednesday, February 15, 2017 1:39 PM

Answers

  • User1494275052 posted

    Unit test should test the smallest unit of code possible. As you have separate functions for login and save, you should write separate tests for those. If you want to write tests that verify the business behaviour, that's BDD. You can do that via a browser, using SpecFlow and Selenium. You could also write code-only tests that test things end-to-end. Those would be integration tests. They allow you to test the business requirement within a single test, but wouldn't be unit tests.

    Also, never call a test of any description "scenario1" - the test name is one of the most important parts of a test. It should be immediately clear what the test is SUPPOSED to be testing from the name alone. If it isn't, and the test fails in 6 months' time (for example), you won't know whether the test or the code is at fault. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2017 1:50 PM
  • User1494275052 posted

    The way I structure my unit tests is as follows:

    • Either create a single Tests project for the solution, or a Tests project per project
    • Within the Tests project, create a Test fixture for each class being tested - so, if you have a class called LoginHandler.cs, create a class called LoginHandlerTests.cs
    • Create an individual test for each scenario of functionality that you need to verify against that class only - for your login code, you might have a test for a valid login, a test for missing username, a test for missing password, a test for invalid credentials, etc.
    • Name each test in terms of what it's testing. I follow the Given-When-Then format, which helps make things very clear. For instance: GivenALoginHandler_WhenLoggingInWithoutAUsername_ThenTheHandlerShouldReturnAMissingUsernameError - yes, test names can be quite long, but it's crystal clear what's being tested. This helps mnassively on large projects
    • Repeat for each class that you need to test
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2017 3:15 PM

All replies

  • User1494275052 posted

    Unit test should test the smallest unit of code possible. As you have separate functions for login and save, you should write separate tests for those. If you want to write tests that verify the business behaviour, that's BDD. You can do that via a browser, using SpecFlow and Selenium. You could also write code-only tests that test things end-to-end. Those would be integration tests. They allow you to test the business requirement within a single test, but wouldn't be unit tests.

    Also, never call a test of any description "scenario1" - the test name is one of the most important parts of a test. It should be immediately clear what the test is SUPPOSED to be testing from the name alone. If it isn't, and the test fails in 6 months' time (for example), you won't know whether the test or the code is at fault. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2017 1:50 PM
  • User-419562455 posted

    Thanks for reply,

    So If i create one unit test case "Login_AddNewUser_Test", which contains both functionality. In terms of other cases it will contains 4 or 5 function combined in one test case then is it standard way to write unit test case or am i go in wrong direction from concept of unit test cases?

    Please guide.

    Wednesday, February 15, 2017 2:14 PM
  • User1494275052 posted

    The way I structure my unit tests is as follows:

    • Either create a single Tests project for the solution, or a Tests project per project
    • Within the Tests project, create a Test fixture for each class being tested - so, if you have a class called LoginHandler.cs, create a class called LoginHandlerTests.cs
    • Create an individual test for each scenario of functionality that you need to verify against that class only - for your login code, you might have a test for a valid login, a test for missing username, a test for missing password, a test for invalid credentials, etc.
    • Name each test in terms of what it's testing. I follow the Given-When-Then format, which helps make things very clear. For instance: GivenALoginHandler_WhenLoggingInWithoutAUsername_ThenTheHandlerShouldReturnAMissingUsernameError - yes, test names can be quite long, but it's crystal clear what's being tested. This helps mnassively on large projects
    • Repeat for each class that you need to test
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 15, 2017 3:15 PM