abseil / abseil/abseil-py

Test cases should be parameterizable based on command-line flags (and other things)

未關閉
#242 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
2.5k
分支
279
平均合併
2 天 1 小時
30 天內合併 PR
1

描述

Enumeration of the cases for a parameterized test method in an `absl.testing.parameterized.TestCase` is eager: it happens at the point where the class is defined.

Under normal circumstances, this is before `googletest.main()` has been called, meaning that the sequence of cases cannot depend on the values of flags defined by `absl.flags` (or, indeed, on anything that is not meaningfully defined at the point that the `TestCase` class is created).

Thus, the following structure will not work:

```python
from absl import flags
from absl.testing import absltest
from absl.testing import parameterized

MY_FLAG = flags.DEFINE_bool(...)

def _gen_cases():
return (x for x in ...) if MY_FLAG.value else (y for y in ...) # But MY_FLAG won't have been set yet

class MyTest(parameterized.TestCase):

@parameterized.parameters(_gen_cases()):
def test_my_condition(self, case):
...

if __name__ == '__main__':
absltest.main()
```

### Possible Fix

Either of two changes could fix this: making command-line flag parsing more eager, or making test case enumeration lazier. The latter seems simpler.

Each of `parameterized.parameters()`, `parameterized.named_parameters()`, `parameterized.product()` and so on could be widened to accept, in addition to the types that it currently accepts, a `callable` that returns one of those types. If it finds its argument is callable, it could defer enumeration of the cases until some point after command-line flags have been parsed.

Then the above snippet could be amended:

```python
@parameterized.parameters(_gen_cases): # Note the generator is not called at this point
def test_my_condition(self, case):
```

貢獻指南

開啟貢獻指南

研究方向

The issue is about making test case enumeration lazy in absl.testing.parameterized. Start by reading the parameterized module in absl/testing/parameterized.py to understand how parameters are currently enumerated. Look at the decorators like parameters, named_parameters, and product. The fix involves modifying these decorators to accept a callable and defer enumeration until after flag parsing. Check how googletest.main() and flag parsing interact. Write a test that uses a flag to generate different test cases to verify the change works.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
testing
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。