[TestMethod("My Test Name")] can be use to change the name of a test in MSTest.
DynamicData attribute can be used to run the test on a collection of object where you can customize the datadisplayname for each run.
Unfortunately, when you try to use both for the same test like below, the test is not affected by the display name set.
Any idea if it's possible to get the two attributes working at the same time.
[TestMethod("My Test")]
[DynamicData(nameof(MyTestObject), DynamicDataSourceType.Property, DynamicDataDisplayName = nameof(GetTestDisplayName))]
Thanks
Related
In my testing scenario, I've set up a few parameterized tests in order to test various data inputs to a function. I'm also using Allure to generate my test report. My issue is that if I set Allure attributes for things like suite and sub suite for instance in the test class itself, it shows the same static data for every test entry in the report. What I'd like to do is to have the sub suite attribute read something like "Test data for input " and then either input 1, 2, 3, etc., depending on which input is being ran through at the time. The input object does contain an index number property, so getting the specific input number isn't an issue, I just need a way to write that to the Allure attribute.
The C# based allure implementation I'm using does have an AllureLifecycle class, which has in instance property (Which from what I understand should give access to the currently running test instance), and then a UpdateTestCase method. What I'm not sure of though is whether the suite and attributes like it can even be updated using this method, or if they are required to be set via data annotations.
What I guess I'm trying to figure out is if what I want to do is even possible? Am I on the right track here?
I have an ASP.NET application (a basic form where I capture some input) in VS2017.
One of the fields on the form is Mark, which is an integer.
I have the following block of code for the Mark, in my .cs file.
[Display(Name = "Mark")]
[Range(1, 10, ErrorMessage = "Mark must be between 1 and 10")]
public int Mark{ get; set; }
I've created a MSTest project for this application to write Unit Tests.
My question is, do you write Test Cases for this block to verify the input value is in the expected range?
If yes, how you write that?
I've started with this.
[DataRow(0, "Mark must be between 1 and 10")]
[DataRow(11, "Mark must be between 1 and 10")]
[DataTestMethod]
public void TestMark_IsMarkValid_NotValid(int mark, string expectedMsg)
{
//Arrange
Student testStudent = new Student();
testStudent.Mark = mark; //this does not throw any error, although the assigned value is outside of the defined range. But it looks like the range validation only applies to the webform.
//Act
string actualMsg = "Mark must be between 1 and 10"; //this is not correct. I was thinking to capture in the actual result the error message yield by the Range validation, but assigning a value outside range doesn't yield any error message.
//Assert
Assert.AreEqual(expectedMsg, actualMsg);
}
Now, not sure if that block should be in scope for unit testing. If it should be, I have a feeling the approach I've taken is not correct.
Any thoughts, please?
Many thanks,
Cosmin
Interesting question. I'm not certain that there is a definitively correct answer. But here are my thoughts :
1) "Mark" is a property. We don't need to unit test a property because Microsoft have already tested that properties work.
2) The attributes do not affect the property, but provide information about the property that others can use. That is why your unit test passes the test for the value. The form uses the attributes, your unit test does not. That is why your unit test can assign any value.
3) If you really want to limit the value of the variable then limit it in the Student class - either by a setter or else explicit get/set and a backing variable - you can never trust any data submitted by the browser.
4) Testing a UI is not easy. We have a test team that tests by hand. We have tried several tools but none is outstanding. Keep your business logic out of your UI and put it in business classes where it can be easily tested.
So, to answer your question, I personally would not test that the attribute functions in a unit test.
I hope that helps.
Testing team will create test cases in XLS (100+ cases)
I have to create the Test Methods for the each cases dynamically at run time.
I can write the logical part to validate whether the test case is pass or fail, but the only thing I want is how to create the test methods at run time. I am using the Xunit Test project in C#.
Suggest a solution to overcome the problem.
I would try to do some kind of row-testing:
Each line would be a different test run.
[Theory,
InlineData("1", "Description 1", ...),
InlineData("2", "Description 2", ...),
InlineData("3", "Description 3", ...)]
public void Can_get_correct_age_for_date(string sno, string description, ...)
{
// you can access the paramaters here
Console.WriteLine(sno);
Console.WriteLine(description);
// Assert Logic
Assert.Equal(...);
}
In your scenario i think you need to rearrange your Excel file or write yourself some kind of "custom excel parser".
Do you get the idea?
I am using Visual Studio 2015 update 3 and TFS 2015 update 3. Below is an example of a test case (psuedocode).
[TestMethod]
[DataSource(PROJECTNAME, TFSURL, TESTCASENUMBER, DataAccessMethod.Sequential)]
public void Test()
{
OpenUrl("someurl");
Assert.IsTrue(Url is displayed);
}
The TESTCASENUMBER is linked to a test case on TFS and does not contain any parameters, so the table is empty. Since the table is empty the test will skip with the message 'Table TESTCASENUMBER is empty'.
Currently the workaround I have been using is to add a dummy parameter to the test case so the test will run. Is there a better workaround than this?
Instead of use DataSource, you can simply associate the test method in a TestCase, check the screenshot below:
In this way, when the automated tool run this TestCase, the test method is linked. You can check article below for more information: https://msdn.microsoft.com/en-us/library/dd380741(v=vs.120).aspx
There is a description entry for Unit Tests in Visual Studio. Is it possible to modify a test description after creation?
[TestMethod]
[Microsoft.VisualStudio.TestTools.UnitTesting.Description("Test Case Description")]
public void EnsureTestCaseValid()
{
}
The Description column in the Test View is readonly, but if you select a test and look in the Properties window, you'll find that the Description property is editable. This will add a [Description("string")] attribute to the test.