question

NewCodee-7655 avatar image
0 Votes"
NewCodee-7655 asked NewCodee-7655 commented

How to test Environment.test() in c#

Is there a way to test method crash in C#?. I need an implementation method in c# for ExpectedSystemExit(available in Java) and ASSERT_DEATH(available in google test).

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Paul-5034 avatar image
0 Votes"
Paul-5034 answered NewCodee-7655 commented

When you say method crash do you mean to test if the code you're calling throws an exception, or do you mean if the test host program has exited?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I want to test the condition where the test host program exits with a non-zero exit code.

0 Votes 0 ·

If the test host exits then it wouldn't be able to test it's own exit code. It only really makes sense to test the exit code of another process.

You could spawn a new process using the Process class:
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=net-5.0

 var process = new Process();
 process.StartInfo = ...
 process.Start();
 process.WaitForExit();
    
 Assert.Equal(1, process.ExitCode); // Exited with error
0 Votes 0 ·

Thank you @Paul-5034. This helps..!

0 Votes 0 ·