Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Copy Locally - Force Rebuild Every Time a Test is Run?
hughball
#1 Posted : Friday, June 20, 2014 12:43:44 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/20/2014(UTC)
Posts: 3

Was thanked: 2 time(s) in 2 post(s)
Good afternoon!

I've got a specific issue in the way that a project is being tested. Here are the steps for ease of viewing:

- I've got a test that moves a file from directory to another.

- The file is set in my project as "Copy Locally - Always". So on build, NCrunch grabs the file and copies it locally.

- My test runs and moves the file - Success

- If I run my test again, it fails because it can't find the file... because the file is only moved over during build, which it isn't doing anymore.

Is it possible to make NCrunch rebuild (and therefore do my Copy Locally) before running a test? Like an attribute or something?

Am I stuck using pre-/post-build events?

Thanks!

Hugh
Remco
#2 Posted : Friday, June 20, 2014 12:55:51 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi Hugh,

Thanks for posting!

Unfortunately there isn't any feature in NCrunch you can use to force a project to rebuild before an individual test is run. Pre/post build events also unfortunately won't work in this situation, as they still require the repeat execution of a build step.

I would, however, consider taking a good look at what this test is doing and perhaps look at ways it could be redesigned.

When creating tests, it's important that they execute without side-effects such as this, as otherwise the results of the test can become very difficult to consistently interpret and the test can be very difficult to maintain/execute.

Is it possible to modify the test to introduce an additional file copy step before the rest of the test executes? You could copy the file to a temporary location, then allow the test to move it as part of its normal behaviour. The initial copy step would allow the test to become repeatable as it does not modify the original file.

It's probably worth looking at cleaning up the moved file at the end of the test run, to keep things tidy. A great pattern when working with tests of this nature is to operate within a temporary directory, for example:

using System.IO;
using NUnit.Framework;

public class Fixture
{
private string _originalDir;
private string _tempDir;

[SetUp]
public void SetUp()
{
_originalDir = Directory.GetCurrentDirectory();
_tempDir = Path.GetRandomFileName();
Directory.CreateDirectory(_tempDir);
Directory.SetCurrentDirectory(_tempDir);
}

[Test]
public void Test()
{
// Place test code here
}

[TearDown]
public void TearDown()
{
Directory.SetCurrentDirectory(_originalDir);
Directory.Delete(_tempDir, true);
}
}
hughball
#3 Posted : Friday, June 20, 2014 3:59:53 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/20/2014(UTC)
Posts: 3

Was thanked: 2 time(s) in 2 post(s)
Thanks for the quick response!

After I'd posted and I had time to reflect, I started wondering why I didn't introduce some code to replace the file. I like the teardown idea (which, I admit, I've never used).

I'll attempt it tonight/tomorrow and advise how it went.

All the best!

Hugh
1 user thanked hughball for this useful post.
Remco on 6/20/2014(UTC)
hughball
#4 Posted : Monday, July 7, 2014 10:55:53 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/20/2014(UTC)
Posts: 3

Was thanked: 2 time(s) in 2 post(s)
Well, I didn't use the TearDown because I didn't want it to run for every test but I did all the file copying and everything within my test. Thanks again!
1 user thanked hughball for this useful post.
Remco on 7/7/2014(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.040 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download