# Doctest/Unittest for fields states Readonly and invisible

**URL:** <https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173>\
**Category:** Developer\
**Created:** [February 22, 2019, 4:58pm UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173 "2019-02-22T16:58:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)\
**Post date:** [February 22, 2019, 4:58pm UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/1 "2019-02-22T16:58:10Z")

</div>

Hi,

May I know how to test for states of Readonly or Invisible?

Best regards

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [February 22, 2019, 5:22pm UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/2 "2019-02-22T17:22:46Z")

</div>

We do not have any facilities for that in the framework.  
You can still instantiate a `trytond.model.EvalEnvironment` and decode the [PYSON](http://docs.tryton.org/projects/server/en/latest/topics/pyson.html) of the states and check the result.  
Indeed I think it will be a great addition to provide a way to get the evaluated states of a field from an instance.

---

<div class="post-metadata">

**Author:** ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)\
**Post date:** [February 22, 2019, 5:31pm UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/3 "2019-02-22T17:31:05Z")

</div>

Hi Ced,

Thank for advice. I will then try to play around with the trytond.model.EvalEnvironment.  
The current test method only left the states test that cannot evaluate using proteus.  
If can provide a way to evaluate the states, the client test will be perfect

Such as:

> > > field.readonly  
> > > True/False

Best regard

---

<div class="post-metadata">

**Author:** ![JCavallo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JCavallo](https://discuss.tryton.org/u/JCavallo)\
**Post date:** [February 25, 2019, 9:54am UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/4 "2019-02-25T09:54:24Z")

</div>

Snippet:

```python
def pyson_result(pyson_expr, target):
    encoder = PYSONEncoder()
    if isinstance(pyson_expr, str):
        the_pyson = encoder.encode(eval(pyson_expr, CONTEXT))
    else:
        the_pyson = encoder.encode(pyson_expr)
    if the_pyson is True:
        return True
    elif the_pyson is False:
        return False

    env = EvalEnvironment(target, Pool().get(target. __name__ ))
    env.update(Transaction().context)
    env['current_date'] = datetime.datetime.today()
    env['time'] = time
    env['context'] = Transaction().context
    env['active_id'] = target.id
    result = PYSONDecoder(env).decode(the_pyson)

    return result

```

---

<div class="post-metadata">

**Author:** ![bala4901](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@bala4901](https://discuss.tryton.org/u/bala4901)\
**Post date:** [February 25, 2019, 11:29am UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/5 "2019-02-25T11:29:19Z")

</div>

May I know where to put this? Doctest or unittest? Or in the model?

---

<div class="post-metadata">

**Author:** ![JCavallo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/48db29/32.png) [@JCavallo](https://discuss.tryton.org/u/JCavallo)\
**Post date:** [February 25, 2019, 1:16pm UTC](https://discuss.tryton.org/t/doctest-unittest-for-fields-states-readonly-and-invisible/1173/6 "2019-02-25T13:16:38Z")

</div>

@bala4901 It is just a utility method we are using to “know” the value of a Pyson expr for a record. You could add it in your unittest file and call it from the unittests.

FYI we did NOT write it in order to actually test states values, but rather because we want to check them at runtime in some cases, so there may be some tweaking to make it work in a unittest context (though I do not think it will be necessary)

Edit : PYSONEncoder, PYSONDecoder, EvalEnvironment and CONTEXT should be imported from trytond.pyson
