471,357 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

NUnit: Test for file system

Hi all,
with NUnit (C#) which is the better way to implement some tests to operate
on a file in the file system?
For instance: file exists, file read and file write.

Thanks in advance.
--
Luigi

Jul 14 '08 #1
5 4377
Luigi,

Why not just use the classes in the System.IO namespace to determine if
the file exists, and to read/write from/to the file?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Luigi" <ci****************@inwind.itwrote in message
news:03**********************************@microsof t.com...
Hi all,
with NUnit (C#) which is the better way to implement some tests to operate
on a file in the file system?
For instance: file exists, file read and file write.

Thanks in advance.
--
Luigi

Jul 14 '08 #2
Use dependency injection or the service pattern to obtain a reference to an
interface that provides these functions, and use mocks to mock them during
testing.

Pete

Jul 14 '08 #3
I'm confused about what you're asking.
bool b = System.IO.File.Exists(@"C:\myfile.txt");
Assert.AreEqual(true,b);
That looks like the basic setup to me.

"Luigi" <ci****************@inwind.itwrote in message
news:03**********************************@microsof t.com...
Hi all,
with NUnit (C#) which is the better way to implement some tests to operate
on a file in the file system?
For instance: file exists, file read and file write.

Thanks in advance.
--
Luigi

Jul 14 '08 #4
Yes, something similar.
Not also verify that the file exists, but also that I can read it.
A full tests for file system access and reading.

Luigi
Jul 15 '08 #5
i od have something like this

public static bool IsFileWritable(string fileName)
{
//write at least one single line into file,
//used by unit test
try
{
//to clear file use
// FileInfo afileinfo = new FileInfo("list.txt");
afileinfo.Delete();
string line = String.Empty;
StreamWriter oSWriter = new StreamWriter(fileName);
oSWriter.WriteLine("TEST:" +
DateTime.Today.ToString("yyMMdd"));
oSWriter.Close();

//Chk segments in created file
StreamReader aSR = new StreamReader(fileName);

line = aSR.ReadLine();
if (line == null) return false;
if (!line.StartsWith("TEST:")) return false;

aSR.Close();
oSWriter.Dispose(); //remove from memory,does not delete file
return true;
}
catch (Exception)
{
return false;
}
}
"Luigi" wrote:
Yes, something similar.
Not also verify that the file exists, but also that I can read it.
A full tests for file system access and reading.

Luigi
Jul 28 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Rilling | last post: by
2 posts views Thread by d2d | last post: by
5 posts views Thread by Naveen Mukkelli | last post: by
20 posts views Thread by Parag | last post: by
6 posts views Thread by tchaiket | last post: by
reply views Thread by angshuman.agarwal | last post: by
2 posts views Thread by JohnGoogle | last post: by
1 post views Thread by nyathancha | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.