Testing Model rules


(Mike Kobit) #1

How do you write tests against @Mutate, @Model, and other rules? I see that internally, Gradle unit tests casts the Project instance returned by ProjectBuilder to ProjectInternal, and then use the getModelRegistry() to realize and tests the rules.

What is the recommended way for plugin authors to write these tests? If it is to use the ProjectInternal (at least for now), what is the recommended way in the future?

(Mark Vieira) #2

This is basically the question asked here. Yes, I’d say this is really the only way to do unit testing right now. There is a possibility we’ll add better fixture support here.

Your other alternative is to use functional testing via TestKit for this and embed your assertions in an actual task.

interface Person {
    String getName()
    void setName(String s)

class Rules extends RuleSource {
    void person(Person p) { }
    void testRule(Person p) {
        p.name = 'Bob'

apply plugin: Rules

model {
    tasks {
        assertModel(Task) {
            doLast {
                assert $.person.name == 'Bob'

(Carlo Luib-Finetti) #3

Excuse my intervention here. I really didn’t study the new model effectively, but just gave it some glimpse in the user manual. My first impressions always are: why does the manual describe the new model using non build types like ‘Person’? Why doesn’t it describe itself in terms and notions of build objects?

(Mark Vieira) #4

This is a very good point. We should be building our examples around more build-related domain objects.