DevLabs > DevLabs Forums > Pex > data driven test Vs Parametrize Unit Test
Ask a questionAsk a question
 

Proposed Answerdata driven test Vs Parametrize Unit Test

  • Monday, October 26, 2009 9:32 PMDeveloper2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Pex team,
    I wondering what is the different between regular unit test that uses data driven test and PEX that uses Parametrize Unit test.
    both are taken a parameter for test and we can use both of them to test another test cases using the same parametrize method

    So what is new in PEX parametrize unit test ?!

    Thanks :)

All Replies

  • Tuesday, October 27, 2009 2:45 AMNikolai TillmannMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Conceptually, both are quite similar: On both cases, you have tests that receive inputs from the outside.

    Advantages of Visual Studio's data driven tests:
    - easy to get externally provided data from a database, or stored in a serialized form on disk.
    - you don't have to write "assumptions" on test inputs, as you can make sure that the data source doesn't provide "illegal" data

    Advantages of Pex' parameterized unit tests:
    - the data doesn't have to be serializable, as Pex generates source code to reconstruct it
    - the generated individual unit tests simplify debugging somewhat

    There are probably other advantages/disadvantages.

    Nikolai Tillmann - Tell us how you use Pex
  • Tuesday, October 27, 2009 3:40 AMPeliMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    You can actually mix both concepts with Pex. On parameterized unit tests, you can use the [PexArguments(...)] to provide tuples of inputs for the test. Pex will execute those before generating new inputs.

    [PexMethod]
    [PexArguments(1,2,3)]
    [PexArguments(0,0,1)]
    public void FooBar(int a, int b, int c) {
        ...
    }
    


    Jonathan "Peli" de Halleux - Give us your input about Pex!
  • Tuesday, October 27, 2009 7:36 AMDeveloper2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I think PEX can generate test inputs for my parameterize method.
  • Tuesday, October 27, 2009 7:38 AMDeveloper2006 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Peli, this is new for me, I think that's what I am looking for, provide dummy inputs and imagine it like it comes from DB.
    could you please illustrate it in more details, give me some examples please :)
  • Tuesday, October 27, 2009 12:48 PMPeliMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Let's see, imagine that you define an interface that contains the method that your DAL is exposing:

        interface IRepository {
            int GetData(int id);
        }

    Instead of passing the actual implementation that calls into the SQL database to your test, you can pass a stub implementation. The simplest would look like this:

        class SimpleRepository : IRepository {
            public int Data;
            public int GetData(int id) { return this.Data; }
        }

    which then you can use in your test like this:

        [PexMethod]
        Test(int data) {
            var repository = new SimpleRepository { Data = data };
            BLLMethod(repository);
        }

    This is basically mock based testing. Pex provides you with automated ways to generate SimpleRepository so follow this tutorial: http://research.microsoft.com/en-us/projects/pex/stubstutorial.pdf to learn more.

    That should get you started...


    Jonathan "Peli" de Halleux - Give us your input about Pex!
  • Tuesday, October 27, 2009 1:15 PMNikolai TillmannMSFT, OwnerUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If you want to learn more about [PexArguments(...)], also take a look at section 2.12 "Seed Values for Fuzzing and to Help Pex" in our patterns paper: http://research.microsoft.com/en-us/projects/pex/patterns.pdf

    Note that you will still need to write assumption, as Pex is just using those values to get fuzzing started, but Pex will come up with different values as well.

    Nikolai Tillmann - Tell us how you use Pex