Model a test result with a dataclass and reject invalid duration values at construction time.
Answer
Declare the data fields with `@dataclass`, then put invariants that depend on initialized fields in `__post_init__`. This keeps generated `__init__`, `__repr__`, and equality behavior while still enforcing domain rules. Raise a specific standard exception such as `ValueError` when the caller provides a negative duration.
Strong answer includes
- uses @dataclass for the record
- validates initialized fields in __post_init__
- raises a clear exception for invalid input
Practical example
Python
Result
Run the code to see the result.
The dataclass generates the constructor and representation; __post_init__ runs immediately after field assignment and enforces the domain rule.
Expected result: TestResult(name='login', status='passed', duration=1.2)