Need advice about which tool to choose?Ask the StackShare community!

pytest

3.7K
291
+ 1
0
TestNG

1.1K
194
+ 1
0
Add tool

TestNG vs pytest: What are the differences?

Introduction

TestNG and pytest are two popular testing frameworks used in software testing. While both frameworks serve the same purpose of automated testing, they have some key differences that set them apart. In this article, we will discuss the six main differences between TestNG and pytest.

  1. Test Execution Model: TestNG follows a suite-test-method hierarchy for test execution. It allows the grouping of tests into classes, which are organized within test suites. On the other hand, pytest follows a more flexible and streamlined approach. It automatically discovers and executes tests based on naming conventions and allows the use of test functions or test classes without any specific hierarchy.

  2. Fixture Management: Pytest has a more powerful and flexible fixture management mechanism compared to TestNG. Fixtures in pytest allow the setup and teardown of test resources, such as creating database connections or setting up test data. TestNG, on the other hand, uses annotations like @BeforeTest and @AfterTest to define setup and teardown methods.

  3. Test Parameterization: Parameterization of tests is more straightforward in pytest compared to TestNG. Pytest allows the use of decorators such as @pytest.mark.parametrize to define multiple test inputs and expected outputs. TestNG uses data providers for parameterization, which require an additional setup for data feeding.

  4. Test Reporting: TestNG provides comprehensive testing reports by default, including passed, failed, and skipped tests, along with time and status. It also supports various report formats like HTML and XML. Pytest, on the other hand, offers minimalistic reporting by default. However, it provides a rich ecosystem of plugins that can generate detailed and customized reports.

  5. Parallel Execution: TestNG has built-in support for parallel test execution, allowing tests to run concurrently on multiple threads or machines. It provides annotations like @Test(threadPoolSize) to specify the number of threads for parallel execution. Pytest also supports parallel test execution through plugins like pytest-xdist. However, it requires additional setup and configuration compared to TestNG.

  6. Test Discovery: Pytest has a powerful test discovery mechanism that automatically discovers and executes tests from a directory or module. It uses naming conventions and explicit test markers to identify and run tests. TestNG, on the other hand, requires manual configuration and explicit inclusion of test classes or methods in test suite XML files for test discovery.

In summary, TestNG and pytest differ in their test execution model, fixture management, test parameterization, test reporting, parallel execution support, and test discovery mechanism. Understanding these differences is crucial in choosing the right testing framework for your project.

Get Advice from developers at your company using StackShare Enterprise. Sign up for StackShare Enterprise.
Learn More
- No public GitHub repository available -

What is pytest?

A framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. It is a mature full-featured Python testing tool.

What is TestNG?

It is a testing framework designed to simplify a broad range of testing needs, it covers all categories of tests: unit, functional, end-to-end, integration, etc.Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc.

Need advice about which tool to choose?Ask the StackShare community!

What companies use pytest?
What companies use TestNG?
See which teams inside your own company are using pytest or TestNG.
Sign up for StackShare EnterpriseLearn More

Sign up to get full access to all the companiesMake informed product decisions

What tools integrate with pytest?
What tools integrate with TestNG?

Sign up to get full access to all the tool integrationsMake informed product decisions

What are some alternatives to pytest and TestNG?
Jasmine
Jasmine is a Behavior Driven Development testing framework for JavaScript. It does not rely on browsers, DOM, or any JavaScript framework. Thus it's suited for websites, Node.js projects, or anywhere that JavaScript can run.
Cucumber
Cucumber is a tool that supports Behaviour-Driven Development (BDD) - a software development process that aims to enhance software quality and reduce maintenance costs.
Avocado
Avocado is an open source interaction design toolbox built by​ IDEO.​ It ​enables designers to make quick interactive prototypes without writing a line of code.​ ​Built on top of Facebook's Origami framework, Avocado provides ready-to-use patches that can be easily combined to create fully-customized prototypes.​ Official announcement: http://labs.ideo.com/2014/05/27/avocado/
behave
It is behaviour-driven development, Python style. It uses tests written in a natural language style, backed up by Python code.
unittest
It is python’s xUnit style framework. It works much the same as the other styles of xUnit, and if you’re familiar with unit testing in other languages, this framework (or derived versions), may be the most comfortable for you.
See all alternatives