roflmuffin / CounterStrikeSharp

CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
https://docs.cssharp.dev
Other
762 stars 117 forks source link

Proposal: Make Plugins Testable #280

Open yonilerner opened 8 months ago

yonilerner commented 8 months ago

Hey friends, I have a sorta insane idea I want to run by you. Ive been working on my plugin with CounterStrikeSharp and its great, but I do most of my development on macos, so Ive been organizing my code so that much of it can run outside of the game server environment so i can test it on my mac.

My goal was to make my plugin testable without having to run a server. Both because of my personal environment limitations, but also because I think having plugins be testable in general will make them more reliable and improve the quality of plugins.

So I started playing with the idea of having everything that interacts with a server be wrapped in interfaces that would allow my plugin code to be 95% runnable outside the server, with a default implementation of each interface of course requiring the server. And then separately you could have test implementations to run your logic in a testable environment without the server present.

This PR has a proof of concept of the interfaces and a concrete implementation, but no test implementation yet https://github.com/yonilerner/cs2-retakes-allocator/pull/73. Because I cant force CSS classes to implement my interfaces, I have to rewrap everything. Basically having every class (or at least the big ones like CCSPlayerController to start) implement an interface (eg. ICCSPlayerController) that defines all the methods and fields but would then allow tests to mock any of them (saves all the wrapping hassle).

Here are some key lines of code from my example PR that demonstrates the idea:

This is of course not the only way to make plugins testable, just the one I started playing around with. Before I went further on this work, I wanted to pose to the community: Is there general interest in making plugins (and even CounterStrikeSharp itself) more testable? Is this something that people would find value including in CounterStrikeSharp by default, so that any plugin - and CounterStrikeSharp internals - can be easily tested without having to jump through the hoops I did?

Let me know what you think. Thanks!

B3none commented 8 months ago

An interface for the CCSPlayerController would help me write unit tests for the Menus as I'm struggling to mock that class without it. 👍

roflmuffin commented 7 months ago

I have a draft PR #331 which auto-generates interfaces for schema classes; though how we can update the core to use these interfaces without breaking backwards compat is up in the air at this point.

yonilerner commented 7 months ago

Oh this is incredible, Im gonna poke around next time I have a chance. Thank you!