Integration and Unit tests - testing

In the last days I studied about tests with Jest, but i don't understood the next.
When I have integration tests I don't use mock? Mock are used just on unit tests?
UPDATE
Today, in my company, the approach that we follow is: Unit tests always mock all external access data, and integration tests should not mock.
Is interesting, associate integration tests with hlg environment, because you can discovery easily what and where broke software

Mocks are a kind of Test Double - a test-specific replacement of a dependency, for purposes of making automated tests deterministic.
There's no universally accepted formal definition of what constitutes a unit test, but in this context, I find the following definition (essentially my own wording) useful:
A unit test is an automated test that tests a unit in isolation of its dependencies.
This definition, however, conveniently avoids defining what a unit is, but that's less important in this context.
Likewise, we can define an integration test as an automated test that exercises the System Under Test (SUT) with its real dependencies. Thus, instead of replacing the database dependency with a Test Double, the test exercises the SUT integrated with a real database, and so on.
Thus, with this view of integration testing, no Test Doubles are required because all real dependencies are integrated.
There's another view of integration testing that considers integration testing the exercise of various software components (units, if you will) with each other, while still replacing out-of-process resources like databases or web services with Test Doubles. This is often easier to accomplish, and can be a valuable technique, but whether you decide to call these unit tests or integration tests is largely a question of personal preference.
Unfortunately, there's no universally accepted consistent definition of these terms. I usually try to stick with the vocabulary documented in xUnit Test Patterns, which is the most comprehensive and internally consistent body of work on the topic (that I know of).

From ISTQB definition, Integration is “A test level that focuses on interactions between components or systems.”
So you can have integration test between units, or between different components, or between subsystems. You may also integrate system of systems.
You can read unit test in wikipedia.
So you can use unit test framework (mock/stub) to do integration test also, but when integration test of whole application usually requires a full environment setup, which unit test framework can not do.

Here are my 2 cents:
Unit tests - always use mock. The "unit" of test is a method.
Integration tests - never use mock. The "unit" of test is a class.
End-to-end tests - uses the actual program. The "unit" of test is a single "happy path".

Related

If I write tests for database calls then I'm writing unit tests or integration tests?

I am starting to write tests for database calls and queries. But I was wondering, since it doesn't depend on any other function, then writing test for database calls are unit tests?
Edit: This is around a Node.js environment
The database is separate to the application that you are testing, and as such, the tests would be integration tests rather than unit tests.
Note that unit tests are limited to dealing with single pieces of software in isolation. If you explicitly want to unit test your code as it stands (without making actual database calls), you can make use of a mocking framework, such as Moq.

Terminology for integration tests which test each layer and the layers below it (Jenga tests)

In my app, I've developed an automated testing strategy where each layer has some unit tests and some integration tests.
It seems to me that "integration test" is a fairly sweeping term, which is applicable whenever a test involves more than one unit.
For my integration tests, I felt like I had two options:
"test the combination of units in a single layer, and fake out everything else" (e.g in-memory databases or stubbing out the data access layer). Useful to confirm that DI and messaging is wired up correctly.
or
"tests for a given layer should operate against real instances of lower layers" (e.g. hit the database). Useful to gain confidence that the app as a whole will work.
My question is, are there different terms in common use for each scenario? I've started calling the layer-and-below tests "Jenga tests" because they make sure that each layer aligns or stacks properly on the layer below, and the whole tower doesn't fall over.
(p.s. I'm not interested in discussing the pro's and con's of unit tests vs integration tests, or faking the database - just terminology).
I am also confused when people call xUnit test for more than 1 class integration testing.
Wikipedia definition says Integration testing
occurs after unit testing and before validation testing.
On StackOverflow definition is the same.
So the questions becomes "If layer testing a unit test or an integration test?"
I think it is part of unit testing.
Currently I have just one point of reference:
Apache Maven project defines integration test as those after you get your package.
package - take the compiled code and package it in its distributable
format, such as a JAR.
integration-test - process and deploy the
package if necessary into an environment where integration tests can
be run
(To get full list of phases try mvn abracadabra)
For you question, I would suggest to say layer testing and layers stack testing.
I have not come across standard sort terms for that.

Is JUnit black-box or white-box testing?

Is JUnit black-box or white-box testing? I think that it is white-box but i am not sure. I am looking for that but I can't find a clear answer. Even a simple discussion about that would be useful.
The use of the word "unit" in the name of "JUnit" might indicate that JUnit is only suitable for unit testing, and since unit testing is practically synonymous with white-box testing, your suspicion is in the right direction. However, things are slightly more nuanced.
Unit testing, when done according to the book, is necessarily white-box testing in all cases except when testing units that have no dependencies.
In the case of units that have no dependencies there is no difference between unit testing and integration testing, (there are no dependencies to integrate,) therefore the notion of white-box vs. black-box testing is inapplicable.
In the case of units that do have dependencies, unit testing is necessarily white-box testing because in order to test a unit and only that unit you are not allowed to test it in integration with its dependencies, so you have to strip all of its dependencies away, and replace them with mocks, but in doing so you are claiming to know not only what the dependencies of your unit are, but more importantly, in precisely what ways it interacts with them. (Which methods it calls, with what parameters, etc.).
However, despite "unit" being part of its name, JUnit by itself is not limiting you to Unit testing, so it does not impose either a white-box nor a black-box approach. You can use JUnit to do any kind of testing that you like.
It is the addition of a mocking framework such as JMock, Mockito, etc that would make your tests necessarily of the white-box kind.
When I use JUnit I only do what I call Incremental Integration Testing. This means that first I test all units with no dependencies, then I do integration testing on units whose dependencies have already been tested, and so on until everything has been tested. In some cases I substitute some of the dependencies with special implementations that are geared towards testing (for example, HSQLDB instead of an actual on-disk RDBMS) but I never use mocks. Therefore, I never do unit testing except for the fringe case of units with no dependencies, where, as I have already explained, there is no distinction between unit testing and integration testing. Thus, I never do white-box testing, I only do black-box testing. And I use JUnit for all of that. (Or my own testing platform which is largely compatible with JUnit.)
Most of the industry out there appears to be using JUnit to do extensive unit testing (white-box testing) and also their integration (black-box) testing.
Based on white-boxing testing definition JUnit is included in these form of testing, contain these techniques:(e.g. we create a test based on code or provides all information necessary to test all the possible pathways. This includes not only correct inputs, but incorrect inputs, so that error handlers can be verified as well.)

When not to Use Integration Tests

I am writing an application that uses 3rd party libraries to instantiate and make some operations on virtualmachines.
At first I was writing integration tests to every functionality of the application. But them I found that these tests were not really helping since my environment had to be at a determined state, which turned the tests more and more difficult to write. And I decided to make only the unit and acceptance tests.
So, my question ... is/can there be method or a clue to notice when the integration tests are not to be used?? (or I am wrong and on all cases they should be written)
When you don't plan on actually hooking your application up to anything "real"; no real containers, databases, resources or actual services. That's what an integration test is supposed to verify; that everything works properly together.
Integration tests are good to test a full system that has well-defined inputs and outputs that are unlikely to change. If your expected input/outputs change often then maintaining the test may become a maintenance challenge, or, worse, you may choose against improving an interface because of the amount of work that may be required to upgrade the integration tests.
The easy and short rule is: Test in integration test what breaks due to integration and test the rest in unit tests in isolation.
You can even hate integration tests. Writing a unit test for a function that takes only one integer parameter is hard enough. All possible combinations of state (internal and external(time, external systems)) and input can make integration testing practically impossible (for a decent application.)

Are scenario tests groups of sequential unit tests?

I read the Wikipedia article on scenario testing, but I am sad to say it is very short. I am left wondering: are scenario tests a collection of sequential unit tests? Or, perhaps, like a single multi-step unit test? Do many frameworks support scenario tests, or are they covered by unit testing?
If they have nothing to do with automation, what are they?
I don't think there's any fixed relationship between the number and distribution of tests and scenario tests.
I think the most common code-representation of a scenario is a specific set of business data required to support a specific story (scenario). This is often provided in the form of database data, fake stub data or a combination of both.
The idea is that this dataset has known and well-defined characteristics that will provide well defined results all across a given business process.
For a web application I could have a single web-test (or several for variations) that click through the full scenario. In other cases the scenario is used at a lower level, possibly testing a part of the scenario in a functional test or a unit test. In this case I normally never group the tests by scenario, but choose the functional grouping of tests I normally use for unit/functional tests. Quite often there's a method within "Subsystem1Test" that is called "testScenario1" or maybe "testScenarioInsufficientCredit". I prefer to give my scenarios names.
In addition to korsenvoid's response, in my experience scenario based testing will often be automated as it will be included in regression testing. Regression testing is regularly automated as to do it manually does not scale well with regular releases.
In commercial software, good examples of scenerio tests are tutorials included with the user documentation. These obviously must work in each release or be removed from the docs, and hence must be tested.
While you can carry out scenario testing using sequenced unit tests, my guess is that it is more common to use GUI based automation tools. For example, I use TestComplete in this role with a scripting framework to good effect. Scenario tests are typically carried out from a user/client perspective which can be difficult to accurately replicate at a unit level.
IMHO, scenario testing is a testing activity, as opposed to development activity ; hence it's about testing a product, not unit(s) of that product. The test scenario are end-to-end scenarios, using the natural interfaces of the product. If the product has programmatic interfaces, then you could use an unit test framework, or Fitnesse.