DEV Community

GIVEN my article WHEN a reader reads it THEN the reader writes better tests

Jamie Read on March 22, 2019

GIVEN a developer who hates writing tests I'm not one for testing, that's evidenced by my most used side project having around 5% test c...
Collapse
 
stealthmusic profile image
Jan Wedel

I like the idea of given/when/then but I don’t like to use it in Java JUnit e.g. because it’s not support as semantic or syntactic part of the test framework. I’m really opposed to use comments especially together with text explaining what the next lines do.

Some JavaScript test frameworks support it and especially languages that support meta programming.
I want the description to be part of the tests. Otherwise they will be obsolete very quickly...

Collapse
 
jamoyjamie profile image
Jamie Read

This, like many things requires buy in from your team; otherwise, as you say, the comments will be obsolete quickly.

You need to be able to trust that your team will check for things like this in code review so that you can avoid the exact situation you described. If there isn't that trust then this will probably become more of a distraction and hinderance.

It's worth saying though, since the tests test behaviour, the comments shouldn't change that frequently otherwise your system behaviour is changing which will probably then be breaking backwards compatibility. Sure the exact lines of code may change with a refactor etc. but the behaviour described in the comments should ordinarily be static, and if not then obvious how they should be changing buy the commit message etc.

Collapse
 
farshadfarzan880 profile image
FarshadFarzan • Edited

*i use darkrift 2 server for dissonance voice chat but error darkrift server *
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (20)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (48)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (20)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (48)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (20)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (48)
[Error] Client A plugin encountered an error whilst handling the MessageReceived event.
System.IO.EndOfStreamException: Failed to read data from reader as the reader does not have enough data remaining. Expected 2 bytes but reader only has -3 bytes remaining.
at DarkRift.DarkRiftReader.ReadUInt16()
at Dissonance.Integrations.DarkRift2.DarkRift2Helpers.ReadPacket(Message message, Byte[] buffer) in C:\Users\Martin\Documents\Unity\Dissonance\Assets\Dissonance\Integrations\DarkRift2\DarkRift2Helpers.cs:line 31
at DissonanceServerPlugin.DissonanceVoiceServerPlugin.MessageReceived(Object sender, MessageReceivedEventArgs e) in C:\Users\Martin\Documents\Unity\Dissonance\Assets\Dissonance\Integrations\DarkRift2.ServerProject\DissonanceServerPlugin\DissonanceVoiceServerPlugin.cs:line 51
at System.EventHandler1.Invoke(Object sender, TEventArgs e)
at DarkRift.Server.Client.<>c__DisplayClass48_0.<HandleIncomingMessage>g__DoMessageReceived|0()
[Error] Client A plugin encountered an error whilst handling the MessageReceived event.
System.IO.EndOfStreamException: Failed to read data from reader as the reader does not have enough data remaining. Expected 2 bytes but reader only has -3 bytes remaining.
at DarkRift.DarkRiftReader.ReadUInt16()
at Dissonance.Integrations.DarkRift2.DarkRift2Helpers.ReadPacket(Message message, Byte[] buffer) in C:\Users\Martin\Documents\Unity\Dissonance\Assets\Dissonance\Integrations\DarkRift2\DarkRift2Helpers.cs:line 31
at DissonanceServerPlugin.DissonanceVoiceServerPlugin.MessageReceived(Object sender, MessageReceivedEventArgs e) in C:\Users\Martin\Documents\Unity\Dissonance\Assets\Dissonance\Integrations\DarkRift2\.ServerProject\DissonanceServerPlugin\DissonanceVoiceServerPlugin.cs:line 51
at System.EventHandler
1.Invoke(Object sender, TEventArgs e)
at DarkRift.Server.Client.<>c_DisplayClass48_0.g_DoMessageReceived|0()
[Warning] ObjectCacheMonitor 6 AutoRecyclingArray objects were finalized last period. This is usually a sign that you are not recycling objects correctly.
[Warning] ObjectCacheMonitor 6 MessageBuffer objects were finalized last period. This is usually a sign that you are not recycling objects correctly.
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (23)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (23)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (23)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (23)
[Warning] DissonanceVoiceServerPlugin Attempted to relay packet to unknown/disconnected peer (23)

Image description

Collapse
 
jbristow profile image
Jon Bristow

Welcome to BDD style testing!

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski

If you didn't use BDD before you should take a look at the SpecFlow Framework. Have fun.

Collapse
 
jamoyjamie profile image
Jamie Read

That's exactly the framework the last test is written in!

Writing SpecFlow tests like that one was one of the first steps I took in adding tests for this side project. It took a little while to get the framework up and running but I knew that I actually could enjoy writing these forms of tests so it gave me that initial boost moving forward.

I still need to port the tests over to the .NET Core version. Unfortunately I can't get it working on CI until then!

Collapse
 
vip3rousmango profile image
Al Romano

This is great! I'll have to try this out with our team and see it in action.

Collapse
 
jamoyjamie profile image
Jamie Read

Do let me know how well it works for you!