When to use the SetUp attribute

6 ביוני 2010

4 תגובות

My blog has moved. You can view this post at the following address: http://blog.drorhelper.com/2010/06/when-to-use-setup-attribute.html

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

4 תגובות

  1. Yuval7 ביוני 2010 ב 20:49

    Dror, great post and I totally agree. I do wonder about one thing, though – wouldn't you say that having 100 test methods in the same test class is a problem in itself? I'd argue that this indicates that your class-under-test is too big and is probably violating the SRP. Would it not be better to refactor the CUT first, before worrying about the setup methods?

    הגב
  2. ulu7 ביוני 2010 ב 21:07

    I usually use the Setup method to create the context and perform the action (Arrange and Act), and the actual test to verify my assumptions. Most of my test methods become one-liners. I also use FixtureSetup instead of Setup. This way, I only execute it once and can have several checks. Helps a lot with expensive stuff like Web requests and database setup. For example, most of my fixture setups start with creating a test database.

    When a test fails, I know immediately whether it's the initialization phase or the test itself. So, my arguments are the same as yours — I want to be able to look at a failing test and understand why it failed simply by reading it. The difference is, I've got no "class under test", it's rather "context/action under test". So, I'm squeezing the context into the namespace, and the action into the test class name, and it becomes, like, Membership.RegistrationPage.UserEnteredValidData class and ConfirmationEmailIsSent method. Pretty readable.

    So I guess the Setup methods make it worse in your case but certainly help in mine.

    הגב
  3. dhelper7 ביוני 2010 ב 21:56

    yuvmaz – I agree having 100 tests in a single file is a smell, either the tests can be arranged in several files or the class under test does way too much.

    ulu – That's a very interesting way to write the tests, as I understand they are mostly integration tests (database, web etc.) I guess you can either have all of the logic inside the setup method or none at all.
    My only question is when you use a single TestFixtureSetup to initialize the environment for several tests – how do you know if the tests have side effects? if the 3rd test fail – is it because something that happen in the 2nd test?

    הגב
  4. ulu12 ביוני 2010 ב 21:05

    They're mostly integration tests but sometimes I come down to sort of unit tests. Like, when testing the search process, I start with an integration test, then refactor the search logic into some class, and write unit tests for different search parameters. However, I still have separate classes for SearchByName, SearchByPrice etc., although I have one class under test.

    I don't have any side effects since my tests are mostly one-liners. I don't have any actions in them, just queries.

    הגב