This is a migrated thread and some comments may be shown as answers.

Expect Exception in WPF Application

6 Answers 164 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Wetzorke
Top achievements
Rank 1
Wetzorke asked on 19 Dec 2016, 04:18 PM

Hello!

Is there a way to verify that e.g. a Mouse-Click via FrameworkElement.User.Click() throws a particular exception within a WPF Application?

Check within a Unit Test via e.g. MSTest's ExpectedExceptionAttribute or NUnit's Assert.Throws?

Best wishes!

6 Answers, 1 is accepted

Sort by
0
Nikolay Petrov
Telerik team
answered on 22 Dec 2016, 09:32 AM
Hello Christoph,

I would suggest to use a try-catch block and to verify in the catch part the thrown exception using assert statement.

Let me know if that makes sense in your scenario.

Regards,
Nikolay Petrov
Telerik by Progress
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
Wetzorke
Top achievements
Rank 1
answered on 23 Dec 2016, 03:06 PM

Hello Nikolay,

thank you for your response.

I couldn't get anything working from within a Unit Test:

MSTest's ExpectedExceptionAttribute:

[TestMethod]
[ExpectedException(typeof(MyException))]
public void MyTestMethod()
{
    FrameworkElement button = ActiveApplication.MainWindow.Find.ByAutomationId("ButtonId");
    button.User.Click();
    button.User.Click();
}

Result Message: Test method UnitTests.MyUnitTests.MyTestMethod did not throw an exception. An exception was expected by attribute Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute defined on the test method.

 

MSTest V2's Assert.ThrowsException<T>():

[TestMethod]
public void MyTestMethod()
{
    FrameworkElement button = ActiveApplication.MainWindow.Find.ByAutomationId("ButtonId");
    button.User.Click();
 
    var exception = Assert.ThrowsException<MyException>(() => button.User.Click());
    Assert.AreEqual("Do not click twice.", exception.Message);
}

Result Message: Assert.ThrowsException failed. No exception thrown. MyException exception was expected.

 

Here's the Click-Command:

private void OnClick(object obj)
{
    if (++count > 1)
    {
        throw new MyException("Do not click twice.");
    }
}

 

During both tests, the Application crashes because of the Exception (of course). But the Tests fail because they do not recognize this Exception.

Is there a way to expect or extract an Exception within the WPF Application under test, without letting the Windows "Application has stopped working"-Dialog (like this) appear? (I want to avoid having to close any windows manually because I would like to have our TFS execute those Tests without getting cluttered).

If possible, I also want to avoid having testing code or any kind of assertions in my production code, but keep this kind of verification in my UnitTest-Projects.

 

I have attached the project to this post.

Best wishes!

0
Elena
Telerik team
answered on 28 Dec 2016, 02:29 PM
Hi Christoph,

Let me please interfere into this thread since my colleague is out of office for the holidays. Thank you for the provided details. I will review them and get back to you once I gather my observations. Thanks in advance for your understanding! 

Regards,
Elena Tsvetkova
Telerik by Progress
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
Elena
Telerik team
answered on 28 Dec 2016, 05:19 PM
Hello Christoph,

I managed to review your query and would like to place few more questions to clarify the behavior of your application. 

As far as I understand the exception you need to catch is thrown when a button is clicked twice. What is the application behavior in such case - is there are popup window that informs the button is clicked twice?
How does the application crashes? Is it related to the button click? 

In general if the tested application crashes for some reason there is no way how to continue test execution against it. Please elaborate some further details on the expected behavior and what is the difficulty you face with the exception in question. 

Thank you in advance for your time and cooperation! 

Regards,
Elena Tsvetkova
Telerik by Progress
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
Wetzorke
Top achievements
Rank 1
answered on 04 Jan 2017, 05:05 PM

Hello Elena,

thank you for your response.

I wanted to expect an Exception to be thrown under certain conditions, which would actually crash the Application when started regularly (i.e. not from unit test nor from automation). I wanted to achieve something similar to an expected Exception via Assert.Throws, just like a regular .NET Unit-Test, without actually crashing the application when started from the Unit-Test via Automation.

As far as I understand now this seems not possible.

Best wishes!

0
Elena
Telerik team
answered on 09 Jan 2017, 11:14 AM
Hi Christoph,

Your understanding is correct - there is no way for a Test Studio test to catch such an exception but not crash the application. 

Please do not hesitate to contact us in case of any further questions you might have! 

Regards,
Elena Tsvetkova
Telerik by Progress
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Wetzorke
Top achievements
Rank 1
Answers by
Nikolay Petrov
Telerik team
Wetzorke
Top achievements
Rank 1
Elena
Telerik team
Share this question
or