locked
nunit RRS feed

  • Question

  • User1560661405 posted

    hi ,

    can we do unittest for stored procedure usning nunit 

    Wednesday, July 20, 2011 8:08 AM

Answers

  • User-434868552 posted

    my preference is xUnit.net http://xunit.codeplex.com/

    NUnit is more used than xUnit.net AFAIK

    there are other unit testing frameworks too.

    basically, .NET unit testing frameworks interact with .NET assemblies (to keep it simple, think classes and their methods)

    SQL Server operates "out there" as a database engine; likely its internals are not what you'd call .NET (i could be wrong about the internals).

    so, for all intents and purposes, your standard T-SQL stored procedures are too far away for NUnit and xUnit.net to grab onto directly.

    There are some possibilities that would likely work.

    (A) you could use LINQ to SQL or LINQ to Entities in your unit tests to perform queries.

    Example:  pseudo code     /* convert all nulls in column x to value 666; pretend some nulls exist for this example */

           Int32 value666 =. LINQ_query(count(x,666)   /* count existing 666 */

           Int32 countNulls =. LINQ_query(count(x,null)  /* count existing nulls */

           invoke (method_being_tested(parameters...) /* run SP that converts nulls to 666 */

           Int32 value666after =. LINQ_query(count(x,666)   /* count them again */

           if (value666 + valueNulls) notEqual value666after { test.fail }

         comment: the above test would be useless in cases where the test fixture (e.g.: our SQL table) contained all nulls.

    (B) you could use LINQ to SQL or LINQ to Entities in your unit tests to perform queries

         PLUS also use LINQ to SQL or LINQ to Entities instead of stored procedures.

         that is, you eliminate stored procedures altogether.  this is my preference.

         in the above pseudo code example for (A), the method_being_tested would use LINQ instead of executing a SP via a DAL or some other technique.

    (C) possibly you could likely use moq or some other mocking framework to simulate the SP.

    (D) you could test your stored procedures with SQL scripts directly via SSMS.

    TIMTOWTDI =. there is more than one way to do it

    g.    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 10:59 PM
  • User-434868552 posted

    Note:  simply put, regarding (C):  when you mock, you are concerned how the method_being_tested reacts with specific values that the SP might return; you're not testing the SP, rather you are testing expected behaviours of the method_being_tested.

        http://martinfowler.com/articles/mocksArentStubs.html

        http://blogs.clariusconsulting.net/kzu/mocks-stubs-and-fakes-its-a-continuum/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 11:08 PM

All replies

  • User-434868552 posted

    my preference is xUnit.net http://xunit.codeplex.com/

    NUnit is more used than xUnit.net AFAIK

    there are other unit testing frameworks too.

    basically, .NET unit testing frameworks interact with .NET assemblies (to keep it simple, think classes and their methods)

    SQL Server operates "out there" as a database engine; likely its internals are not what you'd call .NET (i could be wrong about the internals).

    so, for all intents and purposes, your standard T-SQL stored procedures are too far away for NUnit and xUnit.net to grab onto directly.

    There are some possibilities that would likely work.

    (A) you could use LINQ to SQL or LINQ to Entities in your unit tests to perform queries.

    Example:  pseudo code     /* convert all nulls in column x to value 666; pretend some nulls exist for this example */

           Int32 value666 =. LINQ_query(count(x,666)   /* count existing 666 */

           Int32 countNulls =. LINQ_query(count(x,null)  /* count existing nulls */

           invoke (method_being_tested(parameters...) /* run SP that converts nulls to 666 */

           Int32 value666after =. LINQ_query(count(x,666)   /* count them again */

           if (value666 + valueNulls) notEqual value666after { test.fail }

         comment: the above test would be useless in cases where the test fixture (e.g.: our SQL table) contained all nulls.

    (B) you could use LINQ to SQL or LINQ to Entities in your unit tests to perform queries

         PLUS also use LINQ to SQL or LINQ to Entities instead of stored procedures.

         that is, you eliminate stored procedures altogether.  this is my preference.

         in the above pseudo code example for (A), the method_being_tested would use LINQ instead of executing a SP via a DAL or some other technique.

    (C) possibly you could likely use moq or some other mocking framework to simulate the SP.

    (D) you could test your stored procedures with SQL scripts directly via SSMS.

    TIMTOWTDI =. there is more than one way to do it

    g.    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 10:59 PM
  • User-434868552 posted

    Note:  simply put, regarding (C):  when you mock, you are concerned how the method_being_tested reacts with specific values that the SP might return; you're not testing the SP, rather you are testing expected behaviours of the method_being_tested.

        http://martinfowler.com/articles/mocksArentStubs.html

        http://blogs.clariusconsulting.net/kzu/mocks-stubs-and-fakes-its-a-continuum/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 11:08 PM
  • User1560661405 posted

    hi,

    thanks

    can we test web application with nunit which is using 3 tier arch

    Thursday, July 21, 2011 3:59 PM
  • User1560661405 posted

    hi,

    thanks

    can we test web application with nunit which is using 3 tier arch

    Thursday, July 21, 2011 3:59 PM
  • User455156504 posted

    Duplicated post. Look at the following thread - http://forums.asp.net/t/1702458.aspx/1?unit+testing

    Thursday, July 21, 2011 5:02 PM