I want to test that a bunch of buttons on screen are responding properly, this amount this increase dynamically thought the project and will be used in more than a single test.
My first try was using
[TestCase]attribute[TestCase("High action - Button")] [TestCase("Low action - Button")] [TestCase("Right action - Button")] [TestCase("Left action - Button")] public void WhenClickOnActionButtons_ActionStreamShouldIncrease (string buttonNameInScene)
But this means that every time that some buttons were added I would need to remember to come back to every test that use the buttons and manually add the new test case
Then I thought that have a container that control the buttons would solve my problem, I will lost the possibility to see which buttons caused the error in the test suite, but this can be fixed just by adding a simple message into the assert
[Test] [Order(1)] public IEnumerator WhenClickOnActionButtons_ActionStreamShouldIncrease () { foreach (var battleActionButton in battleActionButtons) // <- Buttons containers { // Arrange var changedWhenClicked = false; battleActionButton.GetComponent<Button>().onClick.AddListener(() => changedWhenClicked = true); // Act var positionOfButtonInScreen = Camera.main.WorldToScreenPoint(battleActionButton.transform.position); LeftClickOnScreenPosition(positionOfButtonInScreen); yield return null; // Assert Assert.True(changedWhenClicked, $"{battleActionButton.name} didn't pressed correctly"); } }
But now if the button container is empty the test pass, so I thought, let's create a test that runs before it to check if the container is empty
[Test]
[Order(0)]
public void ActionButtonsGroup_IsGreaterThan0()
{
// Arrange
// Act
// Assert
Assert.That(battleActionButtons.Count, Is.GreaterThan(0));
}
But then, if empty, this fails, and the next pass, so I thought, maybe I could make the tests stop running when a test fails, but then I discovered that a test should not rely on other tests, so my question is, how should I handle this kindle of case?