RimWorld

RimWorld

83 ratings
RimTest - Automated Testing Framework
6
2
   
Award
Favorite
Favorited
Unfavorite
Mod, 1.2, 1.3
File Size
Posted
Updated
776.780 KB
Aug 16, 2020 @ 6:00am
Aug 4, 2022 @ 7:41pm
5 Change Notes ( view )

Subscribe to download
RimTest - Automated Testing Framework

Description


Finally unit-testing in rimworld modding!

Write tests about how your mod is supposed to behave > Run the game > Check what failed exactly > Fix the bugs > Expand your mod efficiently and never have to retest the same thing twice anymore!

No need for hours of play testing to find bugs at every change you make, when you can just check everything in seconds!

Can also be used to detect and efficiently bug fix mod incompatibilities, just load mods and check what broke and how! -- assuming the mods are thoroughly tested of course




This is a testing framework with no impact on gameplay, this is compatible with everything.



First, add the RimTest.dll assembly as a reference for your mod.

Make sure to load your mod after RimTest in your About.xml:
<modDependencies> <li> <packageId>latrisstitude.rimtest</packageId> <displayName>RimTest</displayName> <downloadUrl>https://github.com/LaTrissTitude/RimTest/releases</downloadUrl> <steamWorkshopUrl>https://steamcommunity.com/sharedfiles/filedetails/?id=2199316917</steamWorkshopUrl> </li> </modDependencies> <loadAfter> <li>latrisstitude.rimtest</li> </loadAfter>

Template Test Suite to get you started:
using RimTest; using static RimTest.Assertion; namespace yourNamespace{ [TestSuite] public static class RimTestSetup{ [Test] public static void RimTestWorks(){ Assert(true).Not.To.Be.False(); } } }
The framework is documented, here's a cheat sheet to get you started:

Assert(value) followed as many grammar links you want among the following:
  • .Be
  • .Do
  • .Is
  • .Not *
  • .To
* Not also negates your assertion, i.e Assert(x).Not.To.Be.Null() will fail the test if x is null, see below.

Then concluded by the comparison you want among the following:
  • .BetweenExclusive(min, max) ( min < value < max )
  • .BetweenInclusive(min, max) ( min <= value <= max )
  • .EqualTo( check ) ( value == check )
  • .GreaterThan( check ) ( value > check )
  • .LessThan( check ) ( value < check )
  • .SameAs( check ) ( value.Equals(check) )
  • .False() ( value == false )
  • .Null() ( value == null )
  • .True() ( value == true )

AssertFunc(function) follows the same grammar chain rules but only has access to the following comparison:
  • .Throw() ( fails if function throws an Exception when executed )

The GUI is now available as the mod settings screen!



Q: Why the extra work? By the time I'm testing things I'm usually done with my mod
A: You don't know what an update or another mod might break, or worse: Your mod could be going completely bonkers without ever crashing or throwing an error. Testing saves a lot of time by ensuring those situations don't happen, or allowing you to find multiple independent problems instantly.

Q: Unit tests don't find anything unexpected!
A: Of course, you can't detect bugs you were not looking for. However developping your mod around tests instead of the other way around makes it highly unlikely to actually miss bugs. And in the eventual case where you do find an untested bug, that's where you now can write a test to check it doesn't happen anymore! :D

Q: I don't have time to write tests
A: Then don't write tests for everything, it saves time in the long run, but it you can't test everything, test what you feel might be the most susceptible to break or what takes the most time to test manually.

Thanks to the helpful fellas from the Rimworld discord server and dubwise discord server for their handy help when I had some obscure bugs, you rock guys!


Ported to 1.3 by Sakura. This mod is currently unmaintained (I don't have time for modding anymore).
33 Comments
LaTrissTitude  [author] May 22, 2024 @ 9:49am 
@Scorpio: No idea ! :D Could still probably work though
Scorpio Aug 5, 2022 @ 9:42am 
how the hell i just discovered this
LaTrissTitude  [author] Oct 21, 2020 @ 4:37am 
Pausbrak: ah yes I see what you mean, this is on my todo list to be able to automatically set up a context (save / game state) to run specific tests / test suites. For now you have to manually run the suites though.
Right now my goal is to move this GUI from the mod settings into the debug toolbar to allow running tests anytime including pre-game (like during the scenario editor or the character editor)

Strielok: This mod is mainly a bug reporting tool user-wise, the scope of this mod is to be a tool for modders to detect bugs in their C# code, this needs C# coding.
Users shouldn't have to use it unless bugs are detected, in which case the debug log will just automatically appear with a dump of the testing log to share with the mod author by default.
Also it already is perfectly useable with harmony
Striełok Oct 21, 2020 @ 3:11am 
Hey, this stuff is quite good, but i'm see two major problems
1)not user friendly, you need know something about code, i can program in C but in C for microcontrolers, C# is total abstraction for me, and after something about 3 hours i'm just start undersdanding what you want from me, and i'm REALY dont want do this for 500+ mods
2)i think it can be popular for moders, but when you integrate it with harmony, and reduce user-work as much as you can it will be a huge step forward becouse a lot of mods require harmony and autotest feature will be great
Pausbrak Oct 20, 2020 @ 5:21pm 
The issue I'm running into is that my tests fail when they run automatically at game startup.
They're failing because they try to create a Thing but there's no map initialized and the Def database isn't populated. It's not a huge problem as I can just load a debug save and rerun them, but I was hoping to avoid needing to do that if possible.

Alternatively, if there was a way to just specify that specific tests should be delayed until a game is loaded, that'd be fine too. Mostly I want to avoid spurious red errors on startup
LaTrissTitude  [author] Oct 20, 2020 @ 5:12pm 
To provide a full answer I will need more details about your request; however if you're talking about unit testing without needing a running game instance, you probably already can unit test those parts of your mod using the already available full fledged C# testing environments, iirc hugslibs has some of those for example!
Pausbrak Oct 20, 2020 @ 4:43pm 
The update is working great so far, and I'm liking the ability to run tests at runtime. As it turns out, that was a feature I'd need next anyway, since my tests don't work without a game instance.

Still, it bothers me a bit that I need a game instance up and running for what should be unit tests. Do you know of a good way to mock up a Thing for a test? Right now I'm replicating the way the debug menu spawns items but that only works when a world is loaded. I can avoid the issue for most of my tests, but I've got some logic involving pulling data from Things depending on what kind of object they are that really needs testing.
LaTrissTitude  [author] Oct 20, 2020 @ 10:42am 
low priority as I don't dev in C# outside of rimworld modding for now, however it might be handy for an eventual visual studio integration
Madara Uchiha Oct 20, 2020 @ 8:06am 
What are the odds of getting this as a NuGet package as well?
LaTrissTitude  [author] Oct 20, 2020 @ 6:18am 
Update sent! (check the previews for more info about the new interface!)