Unit Test Description question - c#

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.

Related

Using Test displayname with DynamicDataDisplayName in MSTest

[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

why we use description attribute for class property in c#

i saw in some code that use Description attribute for class property and i couldn't find reason of behavior in c# codding
public class sample
{
[Description("description1")]
public string PropertyOnne{ get; set; }
}
for readability of code we can use xml summary for even property and i didn't understand what is difference between summary and Description attribute in class level.
Simple words, you can consider below explanation
The tag is used to generate documentation in XML for your Project at Compile time, this is also used by the visual studio for its intellisense database
The Description attribute used by the designer in order to understand the text, mostly at the bottom of the property window(for reference).
This is for visual designers, they can display the description when referencing them.
Remark from the docs:
A visual designer can display the specified description when referencing the component member, such as in a Properties window. Call Description to access the value of this attribute.
Source: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.descriptionattribute?view=netframework-4.7.2

Unit Testing for VS2017 project

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.

Create Xunit Test Methods dynamicaly based on Excel data

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?

Is it possible to execute a test that takes its data from TFS without any table parameters?

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

Categories