Daily Usage Issues

exception when using Code Contracts

Started by 4rchie on 21,952 views

I've just tried to use Code Contracts in my application. As soon as I've enabled that feature of c# 4.0 I got a big window with message:

---------------------------
nCrunch.TaskRunner40.x86.exe - Assert Failure
---------------------------
Description: Must use the rewriter when using Contract.Ensures
Stack Trace:
at System.Diagnostics.Contracts.Contract.AssertMustUseRewriter(ContractFailureKind kind, String contractKind)

at System.Diagnostics.Contracts.Contract.Ensures(Boolean condition)

at ClassLibrary2.Class1.Dodawanie(Int32 a, Int32 b)

at ClassLibrary2.Class1.TestName()

at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)

at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at Gallio.Common.Diagnostics.ExceptionUtils.InvokeMethodWithoutTargetInvocationException(MethodBase method, Object obj, Object[] args)

at Gallio.Framework.Pattern.PatternTestInstanceState.InvokeTestMethod()

at MbUnit.Framework.TestAttribute.Execute(PatternTestInstanceState state)

at Gallio.Framework.Pattern.PatternTestExecutor.ExecuteTestInstanceAction.Run()

at Gallio.Common.Concurrency.ThreadAbortScope.Run(Action action)

at Gallio.Framework.Sandbox.Run(MarkupDocumentWriter markupDocumentWriter, Action action, String description)

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestInstanceAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestDataItemAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestInstanceAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestDataItemAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestInstanceAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestDataItemAction.Run()

at Gallio.Framework.Pattern.PatternTestExecutor.RunTestAction.Run()

at Gallio.Framework.Pattern.PatternTestController.RunImpl(ITestCommand rootTestCommand, TestStep parentTestStep, TestExecutionOptions options, IProgressMonitor progressMonitor)

at Gallio.Model.Helpers.DelegatingTestController.RunTest(ITestCommand testCommand, TestStep parentTestStep, TestExecutionOptions options, IProgressMonitor progressMonitor)

at Gallio.Model.Helpers.DelegatingTestController.RunTest(ITestCommand testCommand, TestStep parentTestStep, TestExecutionOptions options, IProgressMonitor progressMonitor)

at Gallio.Model.Helpers.DelegatingTestController.RunImpl(ITestCommand rootTestCommand, TestStep parentTestStep, TestExecutionOptions options, IProgressMonitor progressMonitor)

at Gallio.Model.Helpers.SimpleTestDriver.RunTestCommandsAction.Run()

at Gallio.Model.Helpers.SimpleTestDriver.RunAssembly(Assembly assembly, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor)

at Gallio.Model.DotNetTestDriver.ExploreOrRunTask.ExploreOrRun(Type driverType, Object[] driverArguments, String assemblyPath, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor, Boolean useSynchronousMessaging)

at Gallio.Model.DotNetTestDriver.ExploreOrRunTask.RunImpl(Object[] args)

at Gallio.Model.Isolation.IsolatedTask.Run(Object[] args)

at nCrunch.GallioIntegration.NCrunchTestIsolationContext.InvokeTask[TIsolatedTask](IHost host, TIsolatedTask isolatedTask, Object[] args)

at Gallio.Model.Isolation.HostedTestIsolationContext.RunIsolatedTaskInHost[TIsolatedTask](HostSetup hostSetup, StatusReporter statusReporter, Object[] args)

at Gallio.Model.Isolation.HostedTestIsolationContext.RunIsolatedTaskImpl[TIsolatedTask](HostSetup hostSetup, StatusReporter statusReporter, Object[] args)

at Gallio.Model.Isolation.BaseTestIsolationContext.RunIsolatedTask[TIsolatedTask](HostSetup hostSetup, StatusReporter statusReporter, Object[] args)

at Gallio.Model.DotNetTestDriver.ExploreOrRunAssembly(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, RemoteMessageSink remoteMessageSink, IProgressMonitor progressMonitor, String taskName, FileInfo file)

at Gallio.Model.DotNetTestDriver.ExploreOrRun(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor, String taskName)

at Gallio.Model.DotNetTestDriver.RunImpl(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor)

at Gallio.Model.BaseTestDriver.Run(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor)

at Gallio.Model.DefaultTestFrameworkManager.FilteredTestDriver.<>c__DisplayClass1f.<RunImpl>b__1d(ITestDriver driver, IList`1 items, Int32 driverCount)

at Gallio.Model.DefaultTestFrameworkManager.FilteredTestDriver.ForEachDriver[T](MultiMap`2 testFrameworkPartitions, Func`4 func)

at Gallio.Model.DefaultTestFrameworkManager.FilteredTestDriver.RunImpl(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor)

at Gallio.Model.BaseTestDriver.Run(ITestIsolationContext testIsolationContext, TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IMessageSink messageSink, IProgressMonitor progressMonitor)

at Gallio.Runner.DefaultTestRunner.Run(TestPackage testPackage, TestExplorationOptions testExplorationOptions, TestExecutionOptions testExecutionOptions, IProgressMonitor progressMonitor)

at nCrunch.TestExecution.Frameworks.Gallio.GallioTestFramework.#=qP8mqIFwTfEa9WIBfpa5D3qCoKglIKVshOQCFQ$7b7dw=.#=qMvas4948MaQS4aPjrWtf2g==()

at #=qmdEAMK_c0wUkjl8QiR_0B$SqavkVGH8_WBLRdXnGACXX4ij_QGiQi6Yyi3LkqyId.#=qM5vw4cReHtB0oIZAADXisWrKqcIWrMteVT2lL5CSY$Y=(Action #=qQG$$IEqJNd7U57rti2oO0eMOr5L73KqDbcsBxL9FrLo=)

at nCrunch.TestExecution.Frameworks.Gallio.GallioTestFramework.RunTests(TestOutput output, IList`1 testsToRun, MasterExecutionMapSet masterMapSet)

at nCrunch.TestExecution.TestRunnerThread.#=qh9mHFD74qNv_qxnwJeExOA==(IMasterExecutionMap[] #=qk1TQlxFS2PvZulTUk4olrnb6ZWeHHkBAUGvy2pOCQ04=, IList`1 #=q_93JaZI9Uj9vyZ1g2aaI8Q==)

at nCrunch.TestExecution.TestRunnerThread.#=qMatA7O7HNDNftSAGqk$zXwsRyZMSQlaG4AK$V7qYeZE=()

at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

at System.Threading.ExecutionContext.runTryCode(Object userData)

at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)

at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Threading.ThreadHelper.ThreadStart()



Press RETRY to attach debugger

---------------------------


Regards,
4rchie
Does it make any difference if you turn on the 'CopyReferencedAssembliesToWorkspace' option for your tests project?
This post has been deleted.
Changing CopyReferencedAssemblies does not solve the problem. I've wrote a little project that shows the error.

http://speedy.sh/AQwSw/ContractFailingWithNCover.zip


Changing CopyReferencedAssemblies throws the big error message box like above.
Excellent thanks - I'll take a look at the sample and will get back to you.
Remco wrote:Excellent thanks - I'll take a look at the sample and will get back to you.


no problem, take your time. It's not stopping me so its not a priority for me and I have'nt seen others to report that :)
I think Code Contracts may also be causing NCrunch to build really slowly for me.

I have quite a simple solution (3 smallish projects) that builds with Code Contracts. I only have runtime contract 7checking enabled for all the projects.

When I enable NCrunch, the NCrunch process build the solution, and then (looking in Task Manager) ccrewrite and then cccheck run, and load the CPU for a very long time.

The end result is that NCruncH takes minutes longer to build and run my tests than Resharper does.

Any suggestions/workarounds? I'd like to be able to use NCrunch with Code Contracts, but at the moment they seem incompatible.

Thanks in advance.

Edited

I'm not sure if there's much that can be done about this.

Code contracts wasn't written with the idea of continuous testing in mind. Mechanically the way it works is actually quite evil - it infers logic across output assemblies being in the same directory, which requires NCrunch to force itself to turn on the CopyReferencedAssembliesToWorkspace setting for all applicable projects in your solution... this in turn means NCrunch is unable to perform properly optimised builds while you make changes, and it will slow your response times.

The only solution I could offer would be to turn off code contracts for your NCrunch builds. You can do this by adding a condition to the code contracts build property in your .proj file by using the $(NCrunch) variable.
Thanks,

I'll give that a try.
Hi Archie,

I've taken a look at the issue you're experiencing with Code Contracts and I'm wondering if this may be due to your code contracts configuration.

When I try running your test solution through any test runner, I receive the same result (as you've described at the top of this thread). I'm far from an expert on code contracts, but is it possible that you need to switch on the rewriter in the code contracts configuration for this to work correctly? I'm not sure what I can do from the side of NCrunch to make this work.


Thanks!

Remco
Im not an expert on code contracts as well. This error shows up when I don't install the Code Contracts Vs adddon so you're probably not far from the truth with the rewriter. However it is strange for me that I get big error message window from NCover. If it is possible I would stream that error to the NCover log instead of throwing it out to the screen.

Regards,
Archie
It looks to me like the error message that's being raised is done so by the code under test itself ... so there isn't really much I can do about this. Though I definitely take your point - it is extremely annoying!
if we talk about my code, then I do not raise any error messages windows so it must be somewhere in the Code Contracts itself...... conclusion: we're screwed :D
Did you have much luck with implementing the build switch to turn it off? There may be a way of making this more sophisticated so as to only use code contracts when it's actually installed - if you can find a build condition that can identify this scenario .. then just turn off the CONTRACTS_FULL property. This will probably help you with other testing frameworks too.
I have similar issue with Code Contract and nCrunch, and I found both of them quite useful for my project.
So I create separate solution configuration in Visual Studio for nCrunch without Code Code Contract, and make nCrunch using only this configuration via Ncrunch->Setting for every project.

Configuration for nCrunch w/o Code Contract

Post a reply

Log in to reply.