Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show/hide test duration in test nodes #1143

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/TestCentric/testcentric.gui/Presenters/DisplayStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ public virtual void OnTestFinished(ResultNode result)
{
int imageIndex = CalcImageIndex(result.Outcome);
foreach (TreeNode treeNode in GetTreeNodesForTest(result))
{
_view.SetImageIndex(treeNode, imageIndex);
UpdateTreeNodeName(treeNode);
}
}

public virtual void OnTestRunStarting()
{
_view.ResetAllTreeNodeImages();
UpdateTreeNodeNames();
}

public virtual void OnTestRunFinished()
Expand Down Expand Up @@ -132,7 +136,8 @@ protected TreeNode MakeTreeNode(TestGroup group, bool recursive)

public TreeNode MakeTreeNode(TestNode testNode, bool recursive)
{
TreeNode treeNode = new TreeNode(testNode.Name);
string treeNodeName = GetTestNodeDisplayName(testNode);
TreeNode treeNode = new TreeNode(treeNodeName);
treeNode.Tag = testNode;

int imageIndex = TestTreeView.SkippedIndex;
Expand Down Expand Up @@ -171,6 +176,46 @@ public string GroupDisplayName(TestGroup group)
return string.Format("{0} ({1})", group.Name, group.Count());
}

private string GetTestNodeDisplayName(TestNode testNode)
{
string treeNodeName = testNode.Name;

// Check if test result is available for this node
ResultNode result = _model.GetResultForTest(testNode.Id);
if (_settings.Gui.TestTree.ShowTestDuration && result != null)
treeNodeName = testNode.Name + $" [{result.Duration:0.000}s]";

return treeNodeName;
}

/// <summary>
/// Update all tree node names
/// If setting 'ShowDuration' is active and test results are available, show test duration in tree node.
/// </summary>
public void UpdateTreeNodeNames()
{
UpdateTreeNodeNames(_view.Nodes);
}

private void UpdateTreeNodeNames(TreeNodeCollection nodes)
{
foreach (TreeNode treeNode in nodes)
{
UpdateTreeNodeName(treeNode);
UpdateTreeNodeNames(treeNode.Nodes);
}
}

private void UpdateTreeNodeName(TreeNode treeNode)
{
TestNode testNode = treeNode.Tag as TestNode; // Update for TestFixture or Testcase nodes only
if (testNode == null)
return;

string treeNodeName = GetTestNodeDisplayName(testNode);
_view.InvokeIfRequired(() => treeNode.Text = treeNodeName);
}

public static int CalcImageIndex(ResultState outcome)
{
switch (outcome.Status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ public interface ITreeDisplayStrategy
/// Collapse all tree nodes beneath the fixture nodes
/// </summary>
void CollapseToFixtures();

/// <summary>
/// Update all tree node names
/// If setting 'ShowDuration' is active and test results are available, show test duration in tree node.
/// </summary>
void UpdateTreeNodeNames();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public TreeViewPresenter(ITestTreeView treeView, ITestModel model, ITreeDisplayS
_treeSettings = _model.Settings.Gui.TestTree;

_view.ShowCheckBoxes.Checked = _view.CheckBoxes = _treeSettings.ShowCheckBoxes;
_view.ShowTestDuration.Checked = _treeSettings.ShowTestDuration;
_view.AlternateImageSet = _treeSettings.AlternateImageSet;

WireUpEvents();
Expand Down Expand Up @@ -154,6 +155,12 @@ private void WireUpEvents()
_view.CheckBoxes = _view.ShowCheckBoxes.Checked;
};

_view.ShowTestDuration.CheckedChanged += () =>
{
_treeSettings.ShowTestDuration = _view.ShowTestDuration.Checked;
Strategy?.UpdateTreeNodeNames();
};

_view.RunContextCommand.Execute += () =>
{
if (_view.ContextNode != null)
Expand Down
1 change: 1 addition & 0 deletions src/TestCentric/testcentric.gui/Views/ITestTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public interface ITestTreeView : IView
ICommand DebugContextCommand { get; }
IToolStripMenu ActiveConfiguration { get; }
IChecked ShowCheckBoxes { get; }
IChecked ShowTestDuration { get; }
ICommand ExpandAllCommand { get; }
ICommand CollapseAllCommand { get; }
ICommand CollapseToFixturesCommand { get; }
Expand Down
10 changes: 10 additions & 0 deletions src/TestCentric/testcentric.gui/Views/TestTreeView.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/TestCentric/testcentric.gui/Views/TestTreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public TestTreeView()
DebugContextCommand = new CommandMenuElement(this.debugMenuItem);
ActiveConfiguration = new PopupMenuElement(this.activeConfigMenuItem);
ShowCheckBoxes = new CheckedMenuElement(showCheckboxesMenuItem);
ShowTestDuration = new CheckedMenuElement(showTestDurationMenuItem);
ExpandAllCommand = new CommandMenuElement(expandAllMenuItem);
CollapseAllCommand = new CommandMenuElement(collapseAllMenuItem);
CollapseToFixturesCommand = new CommandMenuElement(collapseToFixturesMenuItem);
Expand Down Expand Up @@ -115,6 +116,7 @@ public bool CheckBoxes
public ICommand DebugContextCommand { get; private set; }
public IToolStripMenu ActiveConfiguration { get; private set; }
public IChecked ShowCheckBoxes { get; private set; }
public IChecked ShowTestDuration { get; private set; }
public ICommand ExpandAllCommand { get; private set; }
public ICommand CollapseAllCommand { get; private set; }
public ICommand CollapseToFixturesCommand { get; private set; }
Expand Down
113 changes: 113 additions & 0 deletions src/TestCentric/tests/Presenters/NUnitTreeDisplayStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,121 @@ protected override DisplayStrategy GetDisplayStrategy()
{
return new NUnitTreeDisplayStrategy(_view, _model);
}

[Test]
public void OnTestRunStarting_ResetAllTreeNodeImages_IsInvoked()
{
// Act
_strategy.OnTestRunStarting();

// Assert
_view.Received().ResetAllTreeNodeImages();
}

[Test]
public void OnTestRunStarting_UpdateTreeNodeNames_IsInvoked()
{
// Assert
TestNode testNode = new TestNode("<test-case id='1' name='Test1'/>");
var treeNode = _strategy.MakeTreeNode(testNode, false);
_view.InvokeIfRequired(Arg.Do<MethodInvoker>(x => x.Invoke()));
_view.Nodes.Add(treeNode);

// Act
_strategy.OnTestRunStarting();

// Assert
Assert.That(treeNode.Text, Does.Match("Test1"));
}

[TestCase("Skipped", 0)]
[TestCase("Inconclusive", 1)]
[TestCase("Passed", 2)]
[TestCase("Warning", 3)]
[TestCase("Failed", 4)]
public void OnTestFinished_TreeNodeImage_IsUpdated(string testResult, int expectedImageIndex)
{
// Arrange
TestNode testNode = new TestNode("<test-case id='1' />");
var treeNode = _strategy.MakeTreeNode(testNode, false);
ResultNode result = new ResultNode($"<test-case id='1' result='{testResult}'/>");

// Act
_strategy.OnTestFinished(result);

// Assert
_view.Received().SetImageIndex(treeNode, expectedImageIndex);
}

[Test]
public void OnTestFinished_ShowDurationIsInactive_TreeNodeName_IsUpdated()
{
// Arrange
TestNode testNode = new TestNode("<test-case id='1' name='Test1'/>");
var treeNode = _strategy.MakeTreeNode(testNode, false);
ResultNode result = new ResultNode($"<test-case id='1' result='Passed'/>");
_model.GetResultForTest(testNode.Id).Returns(result);
_view.InvokeIfRequired(Arg.Do<MethodInvoker>(x => x.Invoke()));

// Act
_strategy.OnTestFinished(result);

// Assert
Assert.That(treeNode.Text, Is.EqualTo("Test1"));
}

[Test]
public void OnTestFinished_ShowDurationIsActive_TreeNodeName_IsUpdated()
{
// Arrange
_settings.Gui.TestTree.ShowTestDuration = true;
TestNode testNode = new TestNode("<test-case id='1' name='Test1'/>");
var treeNode = _strategy.MakeTreeNode(testNode, false);
ResultNode result = new ResultNode($"<test-case id='1' result='Passed' duration='1.5'/>");
_model.GetResultForTest(testNode.Id).Returns(result);
_view.InvokeIfRequired(Arg.Do<MethodInvoker>(x => x.Invoke()));

// Act
_strategy.OnTestFinished(result);

// Assert
Assert.That(treeNode.Text, Does.Match(@"Test1 \[1[,.]500s\]"));
}

[Test]
public void MakeTreeNode_ShowDurationIsActive_TreeNodeName_ContainsDuration()
{
// Arrange
_settings.Gui.TestTree.ShowTestDuration = true;
TestNode testNode = new TestNode("<test-case id='1' name='Test1'/>");
ResultNode result = new ResultNode($"<test-case id='1' result='Passed' duration='1.5'/>");
_model.GetResultForTest(testNode.Id).Returns(result);

// Act
var treeNode = _strategy.MakeTreeNode(testNode, false);

// Assert
Assert.That(treeNode.Text, Does.Match(@"Test1 \[1[,.]500s\]"));
}

[Test]
public void MakeTreeNode_ShowDurationIsInactive_TreeNodeName_ContainsTestName()
{
// Arrange
_settings.Gui.TestTree.ShowTestDuration = false;
TestNode testNode = new TestNode("<test-case id='1' name='Test1'/>");
ResultNode result = new ResultNode($"<test-case id='1' result='Passed' duration='1.5'/>");
_model.GetResultForTest(testNode.Id).Returns(result);

// Act
var treeNode = _strategy.MakeTreeNode(testNode, false);

// Assert
Assert.That(treeNode.Text, Does.Match(@"Test1"));
}
}


//public class NUnitTestListStrategyTests : DisplayStrategyTests
//{
// protected override DisplayStrategy GetDisplayStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using TestCentric.Gui.Views;
using System.Collections.Generic;
using System.Linq;
using TestCentric.Gui.Elements;

namespace TestCentric.Gui.Presenters.TestTree
{
Expand All @@ -35,6 +36,20 @@ public void WhenSettingsAreChanged_AlternateImageSet_NewSettingIsApplied(string
Assert.That(_view.AlternateImageSet, Is.EqualTo(imageSet));
}

[TestCase(false)]
[TestCase(true)]
public void WhenContextMenu_ShowTestDuration_IsClicked_SettingsIsUpdated(bool showTestDuration)
{
// 1. Arrange
_view.ShowTestDuration.Checked = showTestDuration;

// 2. Act
_view.ShowTestDuration.CheckedChanged += Raise.Event<CommandHandler>();

// 3. Assert
Assert.That(_model.Settings.Gui.TestTree.ShowTestDuration, Is.EqualTo(showTestDuration));
}

[Test]
public void WhenContextMenuIsDisplayed_GuiMiniLayout_TestPropertiesContextMenu_IsVisible()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public void ShowCheckBoxesIsSet()
_view.ShowCheckBoxes.Received().Checked = showCheckBoxes;
}

[Test]
public void ShowTestDurationIsSet()
{
bool showTestDuration = _settings.Gui.TestTree.ShowTestDuration;
_view.ShowTestDuration.Received().Checked = showTestDuration;
}

//[Test]
//public void StrategyIsSet()
//{
Expand Down
6 changes: 6 additions & 0 deletions src/TestModel/model/Settings/TestTreeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public bool ShowCheckBoxes
set { SaveSetting(nameof(ShowCheckBoxes), value); }
}

public bool ShowTestDuration
{
get { return GetSetting(nameof(ShowTestDuration), false); }
set { SaveSetting(nameof(ShowTestDuration), value); }
}

public string DisplayFormat
{
get { return GetSetting(nameof(DisplayFormat), "NUNIT_TREE"); }
Expand Down