473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Reading test data for NUNIT from a file

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

Jul 13 '06 #1
6 6047
What about using the standard app.config file to store that information?
<tc******@yahoo.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.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

Jul 13 '06 #2
Just in case you were wondering *which* app.config

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

Pranshu

Jul 13 '06 #3
tc******@yahoo.com <tc******@yahoo.comwrote:
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
Jul 13 '06 #4
Jon,
Do you have a NUNIT code example of how you would read the config file
or the resource file?
Thanks.
Jon wrote:
tc******@yahoo.com <tc******@yahoo.comwrote:
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
Jul 14 '06 #5
tc******@yahoo.com <tc******@yahoo.comwrote:
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
Jul 16 '06 #6
Thus wrote Jon Skeet [C# MVP],
tc******@yahoo.com <tc******@yahoo.comwrote:
>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
ne********@joergjooss.de
Jul 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Agnes | last post by:
I create an object named Number , with the function numtowords. i input '100' as parameter, It will return "one hundred". However, any simple way to test this object ???? I only know the old way ...
2
by: d2d | last post by:
How are you doing there folks? I just have this newbie question about how to compile an NUnit test file from command line using "csc.exe" I installed NUnit 2.1 using *.msi file. I was...
5
by: Tom | last post by:
I am new to C# and VS.Net. How do I test inidividual classes? I'm used to Java where a Main method for every class to test it. However, VS sees each Main method as an entry point into the .exe. What...
4
by: geoffblanduk_nospam | last post by:
I have an NUnit test plan with well over 500 tests. I now need to produce a test plan document for these tests - documenting them one by one with Word is a painfull task. I was thinking that...
6
by: Droopy | last post by:
Hi, I want to add unit tests in my application using NUnit. At first, I thought to add unit tests in the class that is tested, enclosed in a conditional attribute to remove these unit tests from...
0
by: Ray Tayek | last post by:
hi, getting a: .\Stdafx.cpp : fatal error C1192: #using failed on 'i: nunit\samples\cpp-sample"' 'The filename, directory name, or volume label syntax is incorrect.' nunit is installed in...
3
by: Anbu | last post by:
Hi All, I have 5 test cases (b, c, a, d & e) , NUnit picks up the in alphabatical order. I want to execute the test case e first before the exection of other function. Is there any way to...
0
by: angshuman.agarwal | last post by:
Hi, I have a owner drawn combo box (using .Net 2.0 Framework). I have written the Nunit test for the same. I am setting the AutoCompleteSource Property (AutoCompleteSource.CustomSource) and...
9
by: Deckarep | last post by:
Hello Group, I actually have two seperate questions regarding Unit Testing with NUnit in C#. Please keep in mind that I'm new to the concept of Unit Testing and just barely coming around to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.