-
Notifications
You must be signed in to change notification settings - Fork 34
Testing Pawn Code
sampctl provides some useful tools for quickly testing code while you work.
The watch flag on sampctl package build
keeps sampctl running and watching
.pwn/.inc files for changes. When it detects a change, it will re-compile the
package.
You can also use this flag on sampctl package run
which will do the above as
well as restarting the server if the build is successful.
This means you just need to hit CTRL+S in your favourite editor and sampctl will automatically recompile your code and run unit tests or restart the server. This significantly speeds up development productivity.
See a demo from a screencast I did for some people on SA:MP discord: https://www.twitch.tv/videos/219788449
There are two runtime modes useful for testing: main
and
y_testing
.
If your code uses y_testing unit testing code, this allows you to quickly see
how many tests have passed and failed in a console window. Pair this with
--watch
and you have a purely automatic unit-test runner.
This is standard in software development. Unit testing is, at the most basic level, testing individual functions and ensuring their output is what you expect.
This method of testing is great because when you make changes to your code in the future, you can check if there were any side effects to your changes that break the tests.
YSI provides the y_testing library for unit testing.
Unit tests are good for some libraries but often you need to test things that require player interactions. This is where "Demo Tests" come from. The name comes from the idea that your tests can also be your demonstration to possible users of how your library works and what features it provides.
sampctl allows a user to run some code you write instantly, no server setup
and no copying of files - all they need to do is execute
sampctl package run --forceBuild --forceEnsure
in the package directory. This
is probably one of the most powerful tools sampctl provides.
This means your tests can also be a demo of your library. Written a tree-chopping library? Great! Make your test code spawn the player in a forest with some trees and a chainsaw and let the player cut down a tree. This can be your own test code while developing the library and it can serve as a demonstration for users.
To create these kinds of tests, simply treat your package entry script as a gamemode and write code as if you were using your library.