MSTest will just tell you which row failed, not the values that were used in the test. In the assertion, I added a string mentioning the values for a and b. This will always be a string, so we need to convert to an integer before we can actually use it. TestContext.DataRow contains the data for the current row, which can be obtained by DataRow. This test will be executed for each row in the CSV file. So “ TestData.csv” becomes “ TestData#csv”. The DataSource attribute contains the provider name, in this case the file name, and for a CSV file we need to also specify a table name which is the filename again, but with the period replaced by a hash sign. Int expected = Convert.ToInt32(TestContext.DataRow) Īssert.AreEqual(expected, actual, $ " a=") Int b = Convert.ToInt32(TestContext.DataRow) arrange int a = Convert.ToInt32(TestContext.DataRow) To make it easier to follow the rest of the discussion, here are the contents of my CSV file: If this is not correct, the name of your first column (“ a” in my case) may contain some weird characters.Īlso set “Copy to output directory” to “ Copy Always” and make sure that the file is also in your source code controls system. You can use a tool like Notepad++ to verify this. Make sure that the file is saved as ANSI.This may also be a cause of problems when you execute your tests on another machine. This may depend on the regional settings on your machine. Make sure that the file is comma-separated, don’t use a semicolon as a separator.In this folder, create a CSV file called “ TestData.csv” with for each interesting combination a row of 3 values: a, b and their GCD. I like to organize my work, so in the test project I created a new folder called “ Data” (not a surprise). On top of that, for some file formats (like Excel, etc.) you’ll need additional software to be installed on your test machine, which may be a problem as well. If the files are not available during the tests, your tests will fail, and if somebody changes the files in a bad way (maybe because they don’t know what the files are for), you’ll be in trouble as well. This means that for your tests, there is now an external dependency. The data source can be an Excel file, a CSV file, an Access table, a SQL Server table, … A lot of data sources can be used, but unfortunately they all live outside of the source file for your test. You then use this file as a data source for your tests. The default way is to create an external file with the test data in it. Testing frameworks like XUnit allow to put the different test cases in the code of the tests, but this is not implemented (yet?) in the Microsoft one. In the Microsoft testing framework, this is not so easy. So we need a way to write the test once, and then pass different sets of parameters in the test. And if we want to change the tests, we have to change it in all the places. In both cases, we have a lot of repetitive work (or copy/paste) to do. This is an open invitation for copy/paste programming! Let’s say you want to test 10 cases, then you’ll need to write 10 tests.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |