title: Running multiple async unit test methods produces an error, but running them individually does not
body: <p>In my WPF MVVM I have two <strong>working when ran individually</strong> unit tests which test two button command methods:</p>
  
  <pre><code>[TestMethod]
  public async Task TestMethod1()
  {
      // Arrange
      var interfaceStub = new StubInterface();
      interfaceStub.Method = () =&gt; "Message";
      var viewModel = new ViewModel(interfaceStub);
  
      // Act
      await Task.Run(() =&gt; viewModel.GenerateCommand.Execute());
  
      // Assert
      Assert.AreEqual("Message", viewModel.Response);
  }
  
  [TestMethod]
  public async Task TestMethod2()
  {
      // Arrange
      var interfaceStub = new StubInterface();
      interfaceStub.Method = () =&gt; "Message";
      var viewModel = new ViewModel(interfaceStub);
  
      // Act
      await Task.Run(() =&gt; viewModel.VerifyCommand.Execute());
  
      // Assert
      Assert.AreEqual("Message", viewModel.Response);
  }
  </code></pre>
  
  <p><code>VerifyCommand</code> and <code>GenerateCommand</code> are actually of type <code>AsyncRelayCommand</code> which implements <code>ICommand</code>, thus they are <code>async void</code> and I have to run them on a separate thread in the test by using <code>Task.Run()</code>. <code>Method1</code> and <code>Method2</code> actually stands for the <code>ExecuteMethod1CommandAsync</code> and <code>ExecuteMethod2CommandAsync</code> commands used by buttons respectively.</p>
  
  <p>Problem is, if I run these tests separately, they both pass. <strong>However, once I try to test them both at the same time</strong>, <code>Assert.AreEqual()</code> fails:</p>
  
  <blockquote>
    <p>Expected: &lt;"Message"> , Actual &lt;></p>
  </blockquote>
  
  <p>This is clearly because one of the tests doesn't wait for the thread to finish and the value is not yet returned at the assertion step. I added the <code>System.Threading.Thread.Sleep(5000);</code> to both test methods and then both tests passed when I ran them together again (which further proves this).</p>
  
  <p>Why does running them individually makes the test methods to wait, but running them separately makes one of them to not wait?</p>
  
  <p>EDIT: When I run them separately, the time it takes: ~140 ms both. When I run them together: the one that passes is ~140 ms again, the other which doesn't is only 90 ms (proves my findings)</p>
  
  <p>EDIT 2: Execute method:</p>
  
  <pre><code>// Executes the action
  public async void Execute(object parameter)
  {
      // Do something
  
       try
       {
           await execute(parameter);
       }
       finally
       {
           // Do something
       }
  }
  </code></pre>
  
  <p><code>execute</code> is a <code>Func&lt;object, Task&gt;</code>. This is apparently where the problem occurs - when execute(parameter) is started, it instantly returns back to the Task in <code>TestMethod</code>. What's the best approach to solve this?</p>