How do I test multiple browsers with selenium and a single NUnit suite and keep it DRY?

前端 未结 5 2030
逝去的感伤
逝去的感伤 2021-02-14 17:41

I\'m looking for a way to reuse one NUnit test suite without duplicating the entire suite for each browser. It seems like I would need a new fixture for each browser. Can I send

5条回答
  •  伪装坚强ぢ
    2021-02-14 17:56

    Good question, plenty of people run into this issue. I'm a fan of injecting my browser into my test case using an IoC container. That lets me put all my browser configuration in a injection 'mudule'

    I use the Java bindings and Guice as my IoC Container, but the principals are the same in .Net. You want a DefaultSelnium field in your class that gets injected. Your tests then use this object and dispose it when they're done. You may find you can inject it right away, or you may need to do the object creation in a setup method. A few things you should watch out for, depending on your unit testing framework:

    • Are your test classes created new for each test? JUnit creates a new instance of the test class for each test to be run. TestNG famously did away with this an reuses test class objects for each contained test. The problem with reuse is your injected DefaultSelenium instance is caried along for the ride, which could lead to problems if your tests are run in parallel, or change browser state.
    • Lazy Load your browser object If your Unit testing tool loads all the test classes right off the bat, it will try to create the browser objects up front, which is pretty resource intensive.

    I'm sure you can Google for yourself better than I can, but these are some DI and NUnit links I thought looked promising.

    NUnit integration tests and dependency injection

    http://buildstarted.com/2010/08/24/dependency-injection-with-ninject-moq-and-unit-testing/

    If you don't like DI I've heard of people using factory methods to generate their browser based on some external setup.

提交回复
热议问题