简体   繁体   中英

MSTest Parameterization of Tests - how do I name the test case?

I want to be able to name my test cases. How do I do that?

[TestClass]
public class RegressionTests
{
    [DataRow(SendProposalRequest.systemError, false, 110, SystemValidations.SystemErrorText)]
    [DataRow(SendProposalRequest.proposalError, false, 22, SystemValidations.PTErrorText)]
    [DataRow(SendProposalRequest.financeError, false, 22, SystemValidations.FTErrorText)]
    [DataRow(SendProposalRequest.noFinance, false, 53, SystemValidations.noFinanceErrorText)]
    [DataRow(SendProposalRequest.annualMileage, false, 22, SystemValidations.annualMileageErrorText)]
    [DataRow(SendProposalRequest.loanAmountBoth, false, 33, SystemValidations.loanAmountBothErrorText)]
    [DataRow(SendProposalRequest.proposalError, false, 22, SystemValidations.PTErrorText)]
    [DataTestMethod]
    
    public void ValidationTests(string requestType, bool valid, int errorCode, string errorText)
    {
        TestSetup.TestSetupMethod(requestType, valid, errorCode, errorText);
    }

I am unclear whether your question is about naming the test method or changing the name of the test as it appears in Visual Studio Test Explorer. I'm going to assume you want to change the name of the test in Test Explorer, or the test result files generated when running tests.


The DataRowAttribute class has a property named " DisplayName ", which you can use to customize the name of that variation of the test:

[TestClass]
public class RegressionTests
{
    [DataTestMethod]
    [DataRow(SendProposalRequest.systemError, false, 110, SystemValidations.SystemErrorText, DisplayName = "System error")]
    //                                                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...
    public void ValidationTests(string requestType, bool valid, int errorCode, string errorText)

The basic format for using this is:

[DataRow(arg1, arg2, ... argN, DisplayName = "Your custom name here")]
//                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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