Matthijs
Matthijs

Reputation: 3170

unittest fails in debug/run all, not in debug/run selected

I have this unittest that fails when I run(or debug) all my unittests, but not when I run the unittest on it's own.

The test:

[TestMethod]
public void IsFullTest()
{
    Assert.IsTrue(WeekPeriod.IsFullWeek);
    Assert.IsTrue(YearPeriod.IsFullYear);
    Assert.IsTrue(DayPeriod.IsFullDay);
    Assert.IsTrue(MonthPeriod.IsFullMonth);
    Assert.IsTrue(QuarterPeriod.IsFullQuarter);
    Assert.IsFalse(MonthPeriod.IsFullWeek);
    Assert.IsFalse(DayPeriod.IsFullYear);
    Assert.IsFalse(WeekPeriod.IsFullDay);
    Assert.IsFalse(QuarterPeriod.IsFullMonth);
    Assert.IsFalse(YearPeriod.IsFullQuarter);
}

And the periods get instantiated in a constructor:

public PeriodTest()
{
    YearPeriod = new Period(new DateTime(2014, 5, 1), new DateTime(2015, 5, 1));
    MonthPeriod = new Period(new DateTime(2014, 5, 1), new DateTime(2014, 6, 1));
    DayPeriod = new Period(new DateTime(2014, 5, 8), new DateTime(2014, 5, 9));
    WeekPeriod = new Period(new DateTime(2014, 5, 5), TimeSpan.FromDays(7));
    QuarterPeriod = new Period(new DateTime(2014, 5, 1), new DateTime(2014, 8, 1));
}

It fails on the first Assert; the Period.IsFullWeek:

public bool IsFullWeek
{
    get
    {
        return !HasTime && (Start != null) && (Start.Value.DayOfWeek == CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek) && (Length == TimeSpan.FromDays(7));
    }
}

Is there any way to discover how the test fails on running all, but not on running running solo?

Additional information:

I debugged the particular test (through debug all) and it is receiving its' data correctly. The IsFullWeek-method generates a wrong value which is why the assert fails. However, it generates the correct value when running only this one test (run selected).

Upvotes: 0

Views: 571

Answers (2)

Peter
Peter

Reputation: 27944

You really have to rewrite you unittest to AAA: Arrange-Act-Assert

  • Arrange: setup your testdata
  • Act: do the test
  • Assert: Check the result

This will help you to figure out what test fails. At this moment you have to start the debugger and even see that test are flaky because of the order. Test that interact with each other are not going to give you a clear overview in what is wrong with your code. To sample a unit test for you:

[TestMethod]
public void WeekTest()
{
     //Arrange
     Period weekPeriod = new Period(new DateTime(2014, 5, 5), TimeSpan.FromDays(7));

     //Act
     bool isweek = weekPeriod.IsFullWeek;

     //Assert
     Assert.IsTrue(isweek );
}

Upvotes: 2

Matthijs
Matthijs

Reputation: 3170

I figured it out. May be a bug in the .Net Framework but this is what happens:

As you can see in IsFullWeek it checks if the Start.Value.DayOfWeek is the FirstDayOfWeek:

CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek

Looking at its declaration here:

public  DayOfWeek FirstDayOfWeek
{ 
    get
    {
         // FirstDayOfWeek is always set in the Calendar setter.
         return ((DayOfWeek)firstDayOfWeek); 
    }

    set { 
          VerifyWritable();
          if (value >= DayOfWeek.Sunday && value <= DayOfWeek.Saturday) { 
          firstDayOfWeek = (int)value;
        } else {
                throw new ArgumentOutOfRangeException(
                        "value", String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_Range"), 
                        DayOfWeek.Sunday, DayOfWeek.Saturday));
          } 
     } 
}

It takes the calendar settings here; and then the assigned first day of the week. When running selected the first day is Monday, as set in my calendar settings. When running all the first day suddenly changes to Saturday, making my test fail because the period starts on Monday + 7 days.

So I know why it fails, why it suddenly changes days is a whole other question.

Upvotes: 1

Related Questions