Is there a way to generalize any function call in a try / catch block?

I am writing a bunch of integration tests for a project. I want to call each individual integration point method enclosed in a try / catch block, so when it fails, I get some feedback for the display, and not just the application crash. I also want to be able to time how much time should be taken, and check the return values ​​when necessary. So, I have an IntegrationResult class with some basic characteristics, results, and time that passed the test:

class IntegrationResult { private StopWatch _watch; public string Description {get;set;} public string ResultMessage {get;set;} public bool TestPassed {get;set;} public string TimeElapsed {get { return _watch == null ? "0" : _watch.Elapsed.TotalMilliseconds.ToString(); } } public void Start() { _watch = StopWatch.StartNew(); } public void Stop() { _watch.Stop(); } } 

The code that I continue to write is as follows:

 IntegrationResult result = new IntegrationResult(); result.Description = "T-SQL returns expected results"; try { result.Start(); SomeIntegrationPoint("potential arguments"); //This is the line being tested result.Stop(); //do some check that correct data is present result.TestPassed = true; result.ResultMessage = "Pulled 10 correct rows"; } catch(Exception e) { result.TestPassed = false; result.ResultMessage = String.Format("Error: {0}", e.Message); } 

I would really like to just pass the SomeIntegrationPoint method as an argument and delegate, or check the results something, but I can’t figure out if this is possible. Are there any frameworks for handling this type of testing, or do you have any suggestions on how I can simplify the code for better reuse? I'm tired of typing this block;)

+4
source share
2 answers

(I assume this is C # as labeled ... although the syntax was not in the question.)

Can you do this. Just change the result class to include:

 class IntegrationResult { string Description { get; set; } string SuccessResultMessage { get; set; } string FailResultMessage { get; set; } public IntegrationResult(string desc, string success, string fail) { this.Description = desc; this.SuccessResultMessage = success; this.FailResultMessage = fail; } public bool ExecuteTest(Func<IntegrationResult, bool> test) { bool success = true; try { this.Start(); success = test(this); this.Stop(); this.ResultMessage = success ? this.SuccessResultMessage : this.FailResultMessage; this.TestPassed = true; } catch(Exception e) { this.TestPassed = false; this.ResultMessage = String.Format("Error: {0}", e.Message); success = false; } return success; } ... 

Then you can change your code for your tests to:

 private void myDoTestMethod(string argumentOne, string argumentTwo) { IntegrationResult result = new IntegrationResult( "T-SQL returns expected results", "Pulled 10 correct rows", "Wrong number of rows received"); result.Execute( r=> { integrationPoint.call(argumentOne, argumentTwo); //do some check that correct data is present (return false if not) return true; }); } 

This can be easily expanded to include your timings as well.

+6
source

Have you viewed the AOP ? PostSharp looks like a good place to start. There is also an example for exception handling aspects.

0
source

All Articles