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

(Unity) Attribute-based BDD-style tests #160

Merged
merged 10 commits into from Apr 12, 2022
Merged

(Unity) Attribute-based BDD-style tests #160

merged 10 commits into from Apr 12, 2022

Conversation

sbergen
Copy link
Owner

@sbergen sbergen commented Apr 10, 2022

Would look something like

// Example of BDD-style tests
[Feature("Restarting the game")]
public class BddStyleTests : BddTest
{
	[Scenario("The game should be restartable by pressing the trigger key")]
	public IBddStep[] Restart() => new[]
	{
		Given("the player has failed", this.FailTheGame()),
		When("the user presses the trigger key", this.MockTriggerInput()),
		Then("the game should be restarted", this.AssertTheGameHasBeenRestarted())
	};

...
}

...which results in the test runner looking like
image

...and the state output like:

[.] Scenario: The game should be restartable by pressing the trigger key (Started 0.14 s ≈ 10 frames ago)
  [.] Given the player has failed (Started 0.14 s ≈ 10 frames ago)
    [✓] Trigger miss CONDITION EXPECTED WITHIN 2.00 s (Completed in 0.11 s ≈ 8 frames)
    [✓] WAIT FOR 1 FRAME(S) (Completed in 0.02 s ≈ 2 frames)
    [✓] Trigger miss CONDITION EXPECTED WITHIN 2.00 s (Completed in 0.00 s ≈ 0 frames)
    [.] WAIT FOR 1 FRAME(S) (Started 0.01 s ≈ 0 frames ago)
    [ ] Trigger miss CONDITION EXPECTED WITHIN 2.00 s
    [ ] WAIT FOR 1 FRAME(S)
  [ ] When the user presses the trigger key
    [ ] Mock trigger input
  [ ] Then the game should be restarted
    [ ] Assert restarted

Unity-only, because we don't depend on NUnit in vanilla C#.

TODO:

  • Update docs and changelog (docs will be updated separately)
  • Put more things in BddTest?
  • Implement tests
  • Make sure all misuse has proper error messages

@codecov
Copy link

codecov bot commented Apr 10, 2022

Codecov Report

Merging #160 (bafc2e4) into main (21eed71) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##              main      #160    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           65        83    +18     
  Lines         1059      1438   +379     
  Branches        47        47            
==========================================
+ Hits          1059      1438   +379     
Flag Coverage Δ
Unity2019.4.14f1 100.00% <100.00%> (?)
Unity2020.3.1f1 100.00% <100.00%> (?)
netstandard 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...beatwaves.responsible/Runtime/Bdd/BddTest.Unity.cs 100.00% <100.00%> (ø)
....responsible/Runtime/Bdd/FeatureAttribute.Unity.cs 100.00% <100.00%> (ø)
...responsible/Runtime/Bdd/ScenarioAttribute.Unity.cs 100.00% <100.00%> (ø)
...waves.responsible/Runtime/Context/SourceContext.cs 100.00% <0.00%> (ø)
...sponsible/Editor/TestOperationStatusWindowState.cs 100.00% <0.00%> (ø)
...ible/Runtime/Unity/UnhandledLogMessageException.cs 100.00% <0.00%> (ø)
...es.responsible/Runtime/TestOperationState.Unity.cs 100.00% <0.00%> (ø)
....responsible/Runtime/Unity/UnityFailureListener.cs 100.00% <0.00%> (ø)
com.beatwaves.responsible/Editor/AggregateLabel.cs 100.00% <0.00%> (ø)
...es.responsible/Runtime/Unity/UnityTestScheduler.cs 100.00% <0.00%> (ø)
... and 9 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 21eed71...bafc2e4. Read the comment docs.

/// </remarks>
/// <seealso cref="FeatureAttribute"/>
/// <seealso cref="ScenarioAttribute"/>
public abstract class BddTest
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is required because AFAIK, NUnit doesn't really support set ups, tear downs etc, if we make it execute a method in any other class. Some discussion here. Also, Unity makes this even more complicated with IEnumerableTestMethodCommand being internal.

Copy link

@webbertakken webbertakken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool and exciting!

Comment on lines 14 to 20
[Scenario("The game should be restartable easily after failure")]
public IBddStep[] Restart() => new[]
{
Given("the player has failed", this.FailTheGame()),
When("the user presses the trigger key", this.MockTriggerInput()),
Then("the game should be restarted", this.AssertTheGameHasBeenRestarted())
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vocabulary feedback:

When looking at this.MockTriggerInput() (also in your opening post), it looks like you're mocking a trigger. But you aren't mocking anything, you're triggering something for real. Now perhaps the thing that's you're triggering is a mock.

I'd argue that in this example it could better be called this.SimulateTriggerKeyInput.

Also; not sure if intended but you're using both "the player" and "the user" for the same actor here, which might not be necessary depending on the language of your team.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that all makes sense!

I should probably review the sample tests as a whole from a consistency and vocabulary perspective.

@sbergen sbergen changed the title (Unity) Experiment with attribute-based BDD-style tests (Unity) Attribute-based BDD-style tests Apr 10, 2022
Base automatically changed from BDD to main April 11, 2022 15:57
@sbergen sbergen marked this pull request as ready for review April 12, 2022 05:39
@sbergen sbergen merged commit cfaba20 into main Apr 12, 2022
@sbergen sbergen deleted the BddAttributes branch April 12, 2022 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants