Reputation: 296
I'm starting a test project on VS 2017 for a new module of an old software. I'm new to TDD and unit test so I wonder if this is the correct way to do this...
I started by testing how objects are added to the lists of the viewmodel class :
[TestClass]
public class RTCM_Config_Test
{
static RTCM_Bouton input = new RTCM_Bouton();
static RTCM_Sortie output = new RTCM_Sortie();
[TestMethod]
public void CreateConfig()
{
//Arrange
//Act
RTCM_Config conf = new RTCM_Config();
//Assert
Assert.IsNotNull(conf);
}
[TestMethod]
public void AddNewInputInConfig()
{
//Arrange
RTCM_Config conf = new RTCM_Config();
//Act
bool isOk = conf.CreateElements(input);
//Assert
Assert.IsTrue(isOk);
}
[TestMethod]
public void AddNewOutputInConfig()
{
//Arrange
RTCM_Config conf = new RTCM_Config();
//Act
bool isOk = conf.CreateElements(output);
//Assert
Assert.IsTrue(isOk);
}
}
And the CreateElements functions in the view model :
public bool CreateElements(RTCM_Bouton btn)
{
if (listButtonsInput == null) return false;
if (btn == null) return false;
if (listButtonsInput.Count >= 10) return false;
return true;
}
public bool CreateElements(RTCM_Sortie sortie)
{
if (listButtonsOutput == null) return false;
if (sortie == null) return false;
if (listButtonsOutput.Count >= 10) return false;
return true;
}
Before test methods, I declare static input and output objects as test parameters, is it the good way to do ? Or should I declare test object in each test method ?
Thank you !
Upvotes: 0
Views: 282
Reputation: 249
For your const test object RTCM_Config, you could use a class variable and initialize it in a test initialize method:
private RTCM_Config _config;
[TestInitialize]
public void TestInitialize()
{
_config = new RTCM_Config();
}
In my opinion, Anything that reduces code is welcome. You could also introduce a class variable for your button, but you have to rearrange it for each test case.
Two your second question, you can use data driven tests.
[DataTestMethod]
[DataRow(60, 13d)]
[DataRow(1800, 44d)]
public void Testcase(int input, double expected)
{
//Do your stuff
}
Happy coding!
Upvotes: 1
Reputation: 316
Im not 100% sure what your asking, but i think your asking how to deal with the parameters your passing, RTCM_Bouton btn & RTCM_Sortie sortie.
public bool CreateElements(RTCM_Bouton btn)
{
if (listButtonsInput == null) return false;
if (btn == null) return false;
if (listButtonsInput.Count >= 10) return false;
return true;
}
This code to me has 4 possible return values or routes, so I would need 4 Tests, typically i follow the naming convention MethodName_Scenario_ExpectedResult
CreateElements_ListButtonsIsNull_ReturnsFalse()
CreateElements_BtnIsNull_ReturnsFalse()
CreateElements_ListButtonsIsNull_ReturnsFalse()
CreateElements_ListInputButtonsIs10orMore_ReturnsTrue()
you should use the //arrange section of your test to "Set Up" the test including anything the method needs to run. Create the class and any objects you need to pass with the minimal implementation (just enough to get the test to pass).
So for example
[TestMethod]
CreateElements_BtnIsNull_ReturnsFalse()
{
//arrange
RTCM_Config conf = new RTCM_Config();
var RTCM_Bouton btn = null;
//act
var result = conf.CreateElements(btn);
//assert
Assert.IsFalse(result);
}
Because RTCM_Bouton needs to be null for some tests and be value for others, i would declare it inside each tests arrange method and not as you have as a global variable.
Upvotes: 2