Are there specific coding guidelines for test automation?



  • Details

    Currently we are trying to find more ways to write better code. Of course, this does not exclude our automated test cases. We are already testing in the dev environment with SonarCube and WhiteSource to find possible errors in branches or in the master source.

    Task:

    We also want to implement code quality guidelines for the test.

    Question:

    • What are the code quality guidelines for testing?
    • Do they differ from the general code quality rules for Java/Python?


  • Summary of Code Quality Guidelines for Automation Code

    • Prioritize english readability for descriptions and code
    • Each test has an assertion at the end of the test
    • Consider mocking and stubbing dependencies
    • Each test should test one and only one thing
    • Use linting and include test specific rules
    • Avoid optimizations for performance
    • Devise a test data strategy
    • Tests don't share state
    • Test the tests

    Yes, the requirements for code quality in application and automation code are different.
    While some factors are common to both areas, emphasis and priority can be quite different.

    I was an application developer for 20 years and have now been an automation engineer for 10 and have learned the following differences:

    Application Code.
    Generally the key attributes here are performance,say for thousands of users at a time so many optimizations are made in the name of performance. Also modularity, extensibility and being DRY are critical approaches. Multi-threading and memory usage are also critical factors.

    Automation Code.
    Performance tends not to be an issue. Frequently the code is being run as being used by one user. Readability, while important in Application code becomes super critical in automation code. Automation code can be thought of as the technical specifications - the documentation - for your system. Making sure the documentation is readable, written with good english, potentially to less technical people, is much more important. Another example of a difference is line and file length which can have different characteristics for test code vs automation code.
    Also you should figure out your test data strategy. Don't go overboard with test data though. Sometimes hard-coding static data is ok, even if it repeats. Be careful not to sacrifice readability by over using extract method and DRY approaches for data. Be careful about artifacts you create and consider removing them as part of the automation code cleanup.
    Tests should not share state meaning they should not depend on each other and should not affect or be affected by other tests or the order that tests run in.
    Finally consider testing the tests, for example make sure page objects have no duplicates or orphans

    One thing that is common to both areas for quality code is the use of strong linting rules, code grading, etc.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2