Home All Groups Group Topic Archive Search About

Reading test data for NUNIT from a file

Author
13 Jul 2006 5:07 PM
tchaiket@yahoo.com
Hey all,

I'm using NUNIT to test our classes.  However, I don't want to hard
code test data into the NUNIT classes.  For example, to test adding a
new Client, I don't want to hard code the Client Name, phone number,
email, etc.  I want to the NUNIT to read this data from a file.  This
way I can change the data in a file easily and test various types of
test data.

I think this can be done using the CONFIG file that NUNIT reads.  Is
there any other way?  Does NUNIT have other method of doing this?
Thanks.
TC

Author
13 Jul 2006 5:35 PM
Francois Bonin [C# MVP]
What about using the standard app.config file to store that information?


<tchai***@yahoo.com> wrote in message
Show quoteHide quote
news:1152810440.204697.285120@b28g2000cwb.googlegroups.com...
> Hey all,
>
> I'm using NUNIT to test our classes.  However, I don't want to hard
> code test data into the NUNIT classes.  For example, to test adding a
> new Client, I don't want to hard code the Client Name, phone number,
> email, etc.  I want to the NUNIT to read this data from a file.  This
> way I can change the data in a file easily and test various types of
> test data.
>
> I think this can be done using the CONFIG file that NUNIT reads.  Is
> there any other way?  Does NUNIT have other method of doing this?
> Thanks.
> TC
>
Are all your drivers up to date? click for free checkup

Author
13 Jul 2006 6:12 PM
pranshu
Just in case you were wondering *which* app.config

http://nunit.com/blogs/?p=9

Pranshu
Author
13 Jul 2006 6:41 PM
Jon Skeet [C# MVP]
tchai***@yahoo.com <tchai***@yahoo.com> wrote:
> I'm using NUNIT to test our classes.  However, I don't want to hard
> code test data into the NUNIT classes.  For example, to test adding a
> new Client, I don't want to hard code the Client Name, phone number,
> email, etc.  I want to the NUNIT to read this data from a file.  This
> way I can change the data in a file easily and test various types of
> test data.
>
> I think this can be done using the CONFIG file that NUNIT reads.  Is
> there any other way?  Does NUNIT have other method of doing this?

In both Java and .NET, I prefer to bundle test data as resource files -
in .NET, this means they are compiled into the test assembly. You can
then use Assembly.GetManifestResourceSource to load the file.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
14 Jul 2006 6:49 PM
tchaiket@yahoo.com
Jon,
Do you have a NUNIT code example of how you would read the config file
or the resource file?
Thanks.


Jon wrote:
Show quoteHide quote
> tchai***@yahoo.com <tchai***@yahoo.com> wrote:
> > I'm using NUNIT to test our classes.  However, I don't want to hard
> > code test data into the NUNIT classes.  For example, to test adding a
> > new Client, I don't want to hard code the Client Name, phone number,
> > email, etc.  I want to the NUNIT to read this data from a file.  This
> > way I can change the data in a file easily and test various types of
> > test data.
> >
> > I think this can be done using the CONFIG file that NUNIT reads.  Is
> > there any other way?  Does NUNIT have other method of doing this?
>
> In both Java and .NET, I prefer to bundle test data as resource files -
> in .NET, this means they are compiled into the test assembly. You can
> then use Assembly.GetManifestResourceSource to load the file.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Author
16 Jul 2006 6:58 AM
Jon Skeet [C# MVP]
tchai***@yahoo.com <tchai***@yahoo.com> wrote:
> Do you have a NUNIT code example of how you would read the config file
> or the resource file?

I'm afraid I don't have time to come up with an example at the moment -
but have you tried it? Just call Assembly.GetManifestResourceStream and
use the returned stream as you would any other stream to load data
from.

If you're having trouble, you could post a test which you'd expect to
work but doesn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
18 Jul 2006 6:03 PM
Joerg Jooss
Thus wrote Jon Skeet [C# MVP],

> tchai***@yahoo.com <tchai***@yahoo.com> wrote:
>
>> Do you have a NUNIT code example of how you would read the config
>> file or the resource file?
>>
> I'm afraid I don't have time to come up with an example at the moment
> - but have you tried it? Just call Assembly.GetManifestResourceStream
> and use the returned stream as you would any other stream to load data
> from.

May I help out :-)

I use this helper class to create and remove temporary files for unit tests.
One can use SetUp() to create a temporary file and TearDown() to remove it.

public static class TestUtility {
    public static FileInfo CreateTestFile(string resourceName) {
        FileInfo file = new FileInfo(Path.GetTempFileName());

        Assembly assembly = Assembly.GetCallingAssembly();
        Stream istream = assembly.GetManifestResourceStream(resourceName);
        using(FileStream ostream = file.Open(FileMode.Create, FileAccess.Write))
{
            byte[] buffer = new byte[0x2000];
            int bytes;
            while((bytes = istream.Read(buffer, 0, buffer.Length)) > 0) {
                ostream.Write(buffer, 0, bytes);
            }
        }

        Console.WriteLine(">> Created test file {0}", file.FullName);
        return file;
    }

    public static void DeleteTestFile(FileInfo file) {
        if(file != null) {
            string fullName = file.FullName;
            try {
                file.Delete();
                Console.WriteLine(">> Deleted test file {0}", fullName);
            }
            catch(IOException ex) {
                Console.WriteLine(">> Cannot delete \"{0}\": {1}", fullName,
ex);
            }
        }
    }
}

Cheers,
--
Joerg Jooss
news-re***@joergjooss.de

Bookmark and Share