简体   繁体   中英

Assert inside Method inside Unit Test

using Microsoft.VisualStudio.TestTools.UnitTesting;

I need to check a single method in my code a number of times with different inputs. I've set up a unit test that captures this, but I want to simplify my testing by putting the unit test's code into a method that I can call in several unit tests with different parameters.

The problem is this: assertions in the method inside the unit test don't send their Messages up to the test for me to view.

Is there any way to fix that by marking my method with something like [TestMethod] or do I have to call Assert from directly inside the unit test?

You can call Assert from anywhere once you've entered the public method that has [TestMethod]. This includes private methods.

Example

[TestMethod]
public void TestAddition(){

testAddition(3,1,2);
testAddition(4,2,2);
testAddition(5,3,2);

}

private void testAddition(int expected, int x,int y){

var actual = x + y;

Assert.AreEqual(expected,actual);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM