Using TestCaseSource with TestCaseData.Returns

623 views
Skip to first unread message

INTPnerd

unread,
Nov 11, 2009, 3:25:02 PM11/11/09
to NUnit-Discuss
In the documentation for the TestCaseSource attribute (http://
www.nunit.org/index.php?p=testCaseSource&r=2.5.2), toward the bottom
of the page it shows how you can return a TestCaseData object and set
an expected return value using a special syntax. I am trying to do
this but if I have my code set up like the example it will not compile
because in the example the DivideTest method specifies a return type
of void even though it is returning a value. I tried changing this to
the return type that I needed, which allows it to compile, but then
NUnit complains that "Method has a non-void return value". How do I
get the test method to return a value that will be tested without any
compile errors or NUnit errors?

Charlie Poole

unread,
Nov 11, 2009, 4:28:41 PM11/11/09
to nunit-...@googlegroups.com
Hi,

The example has a typo. The method should be declared
public int DivideTest(int n, int d)

Just declare your method to return the type you want tested
and - of course - return it. Make sure you specify Returns
on all the cases since NUnit thinks it's an error if you
return something but don't tell it what to do with it.

If this doesn't help, post the code that doesn't work
and we'll see what we can do.

Charlie

PS: I'll fix the typo.

INTPnerd

unread,
Nov 11, 2009, 6:42:42 PM11/11/09
to NUnit-Discuss
Thanks for the help. I had already tried what you said but I got the
"Method has a non-void return value" test fail message from NUnit.
Here is my code (you probably only need to look at the last part of
this where there is the test method and the static class below that):

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

using ACS.Business;
using ACS.Data;
using ACS.BusinessDataBridge;
using ACS.Data.UnitTests;

namespace ACS.BusinessDataBridge.UnitTests.SessionManagerTests
{
[TestFixture]
public class GetNextIdleTooLongTimeTest : DatabaseTestBase
{
private SessionManager _sessionManager;
private TimeSheetsTable _timeSheetsTable;
private ReadsTable _readsTable;
private TimeCardRestraintsTable _timeCardRestraintsTable;
private JobSwitchesTable _jobSwitchesTable;
private BreaksTable _breaksTable;

[SetUp]
public override void SetUp() {
base.SetUp();
_sessionManager = new SessionManager();
_timeSheetsTable = new TimeSheetsTable
(ConnectionSingleton.Instance);
_timeSheetsTable.DeleteAll();
_readsTable = new ReadsTable(new
AlwaysEmptyModifiableReadsRepository());
_readsTable.DeleteAll();
_timeCardRestraintsTable =
new TimeCardRestraintsTable(ConnectionSingleton.Instance);
_timeCardRestraintsTable.DeleteAll();
_jobSwitchesTable = new JobSwitchesTable();
_jobSwitchesTable.DeleteAll();
_breaksTable = new BreaksTable(ConnectionSingleton.Instance);
_breaksTable.DeleteAll();
}

[TearDown]
public override void TearDown() {
_timeSheetsTable.DeleteAll();
_readsTable.DeleteAll();
_readsTable.Dispose();
_timeCardRestraintsTable.DeleteAll();
_jobSwitchesTable.DeleteAll();
_breaksTable.DeleteAll();
base.TearDown();
}

[Test, TestCaseSource(typeof
(GetNextIdleTooLongTimeTestDataProvider),"TestData")]
public DateTime? GetNextIdleTooLongTimeSheet(Session session,
DateTime? expectedNextIdleTooLongTime, DateTime?
expectedLastActivityTime,
ICollection<Read> reads, IEnumerable<TimeCardRestraint> restraints)
{
_readsTable.Insert(reads);
_timeCardRestraintsTable.Insert(restraints);
DateTime? lastActivityTime;
DateTime? nextIdleTooLongTime =
_sessionManager.GetNextIdleTooLongTime(session, out
lastActivityTime);
Assert.That(nextIdleTooLongTime, Is.EqualTo
(expectedNextIdleTooLongTime));
Assert.That(lastActivityTime, Is.EqualTo
(expectedLastActivityTime));
return nextIdleTooLongTime;
}
}

public static class GetNextIdleTooLongTimeTestDataProvider
{
public static IEnumerable TestData {
get {
DateTime? expectedNextIdleTooLongTime = null;
DateTime? expectedLastActivityTime = null;
yield return new TestCaseData(
new Session(),
expectedNextIdleTooLongTime,
expectedLastActivityTime,
new Read[]{},
new TimeCardRestraint[]{})
.SetName("CurrentJobCityIdIsNull")
.Returns(expectedNextIdleTooLongTime);

Charlie Poole

unread,
Nov 11, 2009, 9:47:38 PM11/11/09
to nunit-...@googlegroups.com
Unfortunately, NUnit cannot test for a null return value. The
use of null as the expected return makes it appear as if you
didn't supply any value, hence the message.

Whether this is a limitation to live with or a bug to fix
is something we should discuss here, but currently code
like the following won't work either...

[TestCase(1, null, Result=null)]
public void MyTest(int x, string s)
{
return s == null ? null : s + x.ToString();
}

As I type the example, it does start to look like a bug rather
than a feature. :-(

Charlie

Charlie Poole

unread,
Nov 16, 2009, 4:50:06 PM11/16/09
to nunit-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages