We show how to value both variable generation and energy storage to enable them to be integrated fairly and optimally into electricity capacity markets. We develop theory based on balancing expected energy unserved against costs of capacity procurement, and in which the optimal procurement is that necessary to meet an appropriate reliability standard. For conventional generation the theory reduces to that already in common use. Further the valuation of both variable generation and storage coincides with the traditional risk-based approach based on equivalent firm capacity. The determination of the equivalent firm capacity of storage requires particular care; this is due both to the flexibility with which storage added to an existing system may be scheduled, and also because, when any resource is added to an existing system, storage already within that system may be flexibly rescheduled. We illustrate the theory with an example based on the GB system.